bserve the below code : public class InsurancePolicy { private int policyNumber; private String holderName; private double amount; double calculateBonusAmount(){ //some logic return 0; } } class VehicleInsurancePolicy extends InsurancePolicy { private String vehicleNumber; private int noOfMonths; }The calculateBonusAmount method in the parent class should not be overridden by any of its child classes. Drag and drop the apt choice to satisfy this condition.
Question
bserve the below code : public class InsurancePolicy { private int policyNumber; private String holderName; private double amount; double calculateBonusAmount(){ //some logic return 0; } } class VehicleInsurancePolicy extends InsurancePolicy { private String vehicleNumber; private int noOfMonths; }The calculateBonusAmount method in the parent class should not be overridden by any of its child classes. Drag and drop the apt choice to satisfy this condition.
Solution
To prevent a method from being overridden in a subclass in Java, you can use the final keyword. So, to satisfy the condition, you should modify the calculateBonusAmount method in the parent class InsurancePolicy as follows:
public final double calculateBonusAmount(){
//some logic
return 0;
}
This will ensure that any subclass of InsurancePolicy, including VehicleInsurancePolicy, cannot override the calculateBonusAmount method.
Similar Questions
Observe the below code : public class InsurancePolicy { private int policyNumber; private String holderName; private double amount; public double calculateBonusAmount(){ //some logic return 0; } }This class InsurancePolicy should not be inherited by any other classes. Drag and drop the apt choice to satisfy this condition.
Observe the below code. You have a class InsurancePolicy and another class VehicleInsurancePolicy that inherits InsurancePolicy.InsurancePolicy.javapublic class InsurancePolicy { private int policyNumber; private String holderName; private double amount; double calculateBonusAmount(){ //some logic return 0; } }VehicleInsurancePolicy.javaclass VehicleInsurancePolicy extends InsurancePolicy { private String vehicleNumber; private int noOfMonths; double calculateBonusAmount(){ //some logic return 0; }}Which access specifier can be given to the overridden method calculateBonusAmount.
class Vehicle { protected int wheelsCount; private int doorsCount; public Vehicle(int wheelsCount, int doorsCount) { this.wheelsCount = wheelsCount; this.doorsCount = doorsCount; }}public class Truck extends Vehicle { private double loadCapacity; private int weight; public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) { /* Missing Code */ }}
Declare a method public double calculateInterest() in the class Account. Just declare this method without providing any definition/implementation.Make necessary changes to the class declaration. public class Account{ private int accountNumber; private String holderName; private double balance; // Declare the method calculateInterest }
Predict the outputabstract class Vehicle{ abstract void calcPremium(Number N);}interface Insurance{ abstract void calcPremium (Object O);}class Car extends Vehicle implements Insurance{ public void calcPremium (Object O) { System.out.println("Object"); } void calcPremium (Number N) { System.out.println("Number"); } }public class Test{ public static void main(String[] args) { Vehicle a = new Car(); a. calcPremium (new Integer(121)); Insurance b = new Car(); b. calcPremium (new Integer(121)); Car c = new Car(); c. calcPremium (new Integer(121)); }}Select one:a.NumberObjectNumberb.NumberNumberObjectc.Run time errord.Compile time error
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.