Knowee
Questions
Features
Study Tools

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 over

Question

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 over

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The method calculateBonusAmount() in the VehicleInsurancePolicy class is an example of method overriding in Java. The access specifier for this method in the subclass can be the same as, or less restrictive than, the access specifier in the superclass.

In this case, the calculateBonusAmount() method in the InsurancePolicy class doesn't have an explicit access specifier, which means it has package-private (default) access. This means it can be accessed from any class in the same package.

Therefore, in the VehicleInsurancePolicy class, you can give the calculateBonusAmount() method any of the following access specifiers:

  1. public: The method can be accessed from any class in any package.
  2. protected: The method can be accessed from any class in the same package, and from subclasses in any package.
  3. package-private (default): The method can be accessed from any class in the same package.

You cannot give the method the private access specifier, because this would be more restrictive than the access specifier in the superclass, which is not allowed in Java.

This problem has been solved

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.

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.

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 */ }}

public abstract interface Insurance{public void insuranceDescription(String s);} Which is the correct class? Select one:a.public abstract class Car implements Insurance{ public abstract void insuranceDescription (String s) { }}b.public abstract class Car implements Insurance { }c.public class Car extends Insurance{public void insuranceDescription (Integer i) { }}d.public class Car implements Insurance{public void insuranceDescription (Integer i) { }}

class FourWheeler{    public FourWheeler()    {        System.out.println("Class FourWheeler");    }}class Car extends FourWheeler{    public Car()     {        System.out.println("Class Car");        }}class Audi extends Car{   public Audi()    {        super();        System.out.println("Class Audi");    }}class Driver{    public static void main(String args[])    {        Audi cc=new Audi();    }}

1/3

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.