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
Describe the methods of accessing the data members and member functions of a class in the following cases:
(a) in the member function of the same class.
(b) in the member function of another class.
(c) in the member function of base class.
Can a private member be accessed by
(a) a member of the same class?
(b) a member of other class?
(c) a function which is not a member function?
Write a program by using a class with the following specifications:
Class name — Prime
Data members — private int n
Member functions:
- void input() — to input a number
- void checkprime() — to check and display whether the number is prime or not
Use a main function to create an object and call member methods of the class.
Write a program by using a class with the following specifications:
Class name — Factorial
Data members — private int n
Member functions:
- void input() — to input a number
- void fact() — to find and print the factorial of the number
Use a main function to create an object and call member methods of the class.