Computer Applications

Show with the help of an example how the following base classes can be derived in class bill to fulfill the given requirement:

class elect
{
    String n;
    float units;
    public void setvalue()
    {
        n = "SOURABH";
        units = 6879;

    }
}

Class bill uses data members charge and a member function to calculate the bill at the rate of 3.25 per unit and displays the charge. Class elect is inherited by class bill by using private visibility.

Encapsulation & Inheritance in Java

5 Likes

Answer

Class elect can be derived in class bill as shown below:

class bill extends elect {
    private double charge;
    public void calc() {
        charge = 3.25 * units;
        System.out.println("Charge = " + charge);
    }
}

Answered By

4 Likes


Related Questions