Hi i have three classes as follows ,Class A which is parent ,Class B which is a subclass of A and does not have anything in it , Class C which is a subclass of B and a Test Class with the main function:
public class A {
double pay =0.0;
public double calculatePay(int hoursWorked,double rate) {
pay = hoursWorked*rate;
return pay;
}
}
public class B extends A {
}
public class C extends B {
public double show() {
double paid = calculatePay(10, 1.3);
return paid;
}
}
public class Test {
public static void main(String[] args) {
C c = new C();
System.out.println(c.show());
}
}
My question is how is it that i can call the calculatePay()
method of the grandparent Class A from the Class C without using an instance/object.From my understanding of inheritance i would have thought that i would need an instance of A or C in order to invoke the grandparent method like this even though i know i have inherited the calculatePay method from A i would still require an instance of A or C ?
A a = new A();
a.calculatePay;
or
C c = new C();
c.calculatePay;
I couldnt use the super keyword because A is not a parent to C but a grandparent.Instead i just called it like this and stored it in a double variable:
double paid = calculatePay(10, 1.3);
And when i run the program from main () it works and gives me the correct output according to the logic in the grandparent.Can someone please explain this ? I have seen this in android programming where you call getMethods() from a grandparent within classes which are like 3 levels down the hierarchy