Hi Yogesh,
In case of inheritance we can overrite the one method in another class but can we do the same for variables as well?
Instructor
Yogesh Chawla Replied on 09/09/2021
Yes. We can inherit any functionality from parent class in child classes, be it method, variables, using constructors with super keyword or even inner classes also
See this example for variables and methods:
class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the two numbers is:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference of two numbers is :"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
Thanks Yogesh, Got it.