1

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

GhostCat
  • 137,827
  • 25
  • 176
  • 248
kobewarui
  • 73
  • 1
  • 8
  • Hi - this is the basic model of object oriented inheritance, typically super and sub class are used in your context not parent and child. – Elemental Sep 06 '17 at 07:15
  • "I couldnt use the super keyword because A is not a parent to C but a grandparent" are you saying you found you *can't*, or merely you think it *wouldn't* work? – Andy Turner Sep 06 '17 at 07:31
  • hi thanks for replying but you dint answer my question ? Elemental – kobewarui Sep 06 '17 at 07:36
  • Andy i didnt try it out because from what i read super refers to your direct parent ? which in my case the parent of C is B but i have tried it now and it seems to work also,how is that i thought super is only refers to a call to you parent class not your grand parent andy-turner – kobewarui Sep 06 '17 at 07:39
  • i will GhostCat but if you know a little about what am asking would you kindly share your answer also ? need all the help i can get – kobewarui Sep 06 '17 at 07:42
  • I appreciate the quick comeback. Sure, if you have more specific questions, we can talk about that a bit. – GhostCat Sep 06 '17 at 08:09

2 Answers2

2

As the method is inherited, both would work - you just need the correct syntax:

A a = new A();
a.calculatePay(10, 1.3);

Very same if you used C c = new C() instead.

You don't have to do anything special in Java - as (non-private) methods declared in the A class can be invoked on instances of subclasses.

Beyond that: as of now, there is no sense in having 3 different classes. There is no point in B extending A and being empty. You don't write down B extends A because you can. You do such things because it makes sense to do so - because it helps you solving your problem.

Your problem doesn't require three classes and inheritance at all.

And regarding your actual question: you have to understand that OO languages like Java or C++ typically a "table" approach. Meaning: when you invoke a method on an object, what happens is that "the system" looks into a table for the class of that object - and that table tells it "which" method to call (see here for further reading). In that sense, it doesn't make any difference if you do

this.foo() ... within class Foo

or

someFooObject.foo()

And that is exactly what your code is doing - by just saying calculatePay(10, 1.3) within C.show() ... you are invoking that method on the this reference implicitly.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Hi thanks for replying i just used B inorder to create a hierarchy level upto level 3 .so your saying i can use any method from even a great grandparent without using an instance as long as the method is public and inherited ? because what i did was : calculatePay(int ,double) without using an instance and it worked fine.Note i did not use an instance and that was my main question ? kindly elaborate – kobewarui Sep 06 '17 at 07:31
  • @kobewarui What do you mean by "without using an instance"? By calling `a.calculatePay()` or `c.calculatePay()`, you _are_ using an instance, namely `a` or `c`, respectively. – daniu Sep 06 '17 at 07:35
  • daniu if you look at my question i used a and c to elaborat but i did not instantiate a and c in my code above – kobewarui Sep 06 '17 at 07:51
  • Ghostcat thats true but in my case if you look closely at class C i used calculatePay() without using an object ? was this added implicitly ? if so would it mean that this refers to class C which does not have a calculatePay() method ? – kobewarui Sep 06 '17 at 07:59
  • @kobewarui When you dont use an object explicitly, you call on **this**. That is why I put up that example! A method is **always** invoked "on" an object (at least in Java). When there is no object written down, then **this** is used. – GhostCat Sep 06 '17 at 08:00
  • sorry i never saw your explanation on "this" atleast now it makes sense.Implicitly using this to call calculatePay like : this.calculatePay ? .if so thanks now my question is fully answered – kobewarui Sep 06 '17 at 08:12
0

When you are making an instance of class C, you are already invoking the method calculatePay from class A as it is not overridden in any other class.

Assuming that you have some functionality defined in class B and the calculatePay() method is overridden in class C, so that the code makes sense, You can override the calculatePay in B and call A's calculatePay using super as shown:

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 double calculatePay(int hours,double rate) {    
    return super.calculatePay(hours,rate);
  }
}

public class C extends B { 

  public double calculatePay(int hoursWorked,double rate) {    
          //overridden implementation
  }    

  public double show() {

    //calling calculatePay from B will ultimately call it from A
     double paid = super.calculatePay(10, 1.3);
     return paid;
 }

For the confusion regarding How calculatePay is invoked without using instance of class C or A. To invoke the method you can use Instance of the class or this or simply the name of method.

public class Test {

    String s;

    private String hey() {
        return s;
    }

    public String getS(){
        String sm = this.hey(); // or simply hey();
        // here I could just write hey(); without this

        return sm;
    }
}
  • I understand very well your code above but in mine B is empty and am calling calculatPay from A within C without using an instance and it works fine ,this is what i mean calculatePay(int ,double); ? – kobewarui Sep 06 '17 at 07:53
  • That's due to the inheritance, B extends A, and C extends B... so if you call a method using instance of C and that method is not available in C then it will be looked in B, If it is not found even in B, then it will be looked up in A and so on. So in your case method is being invoked from A. – Raman Kumar Sharma Sep 06 '17 at 07:58
  • But a case can be when you override the method calculatePay in class C, but you still want to use the method defined in class A(implemented differently), then in that case `super` keyword comes in handy. – Raman Kumar Sharma Sep 06 '17 at 08:01
  • sorry for the bother but i did not use an instance of c to call calculatePay() like c.calculatePay(),what i did was simply wrote calculatePay() and passed the parameters and it worked.How ? – kobewarui Sep 06 '17 at 08:03
  • because calling calculatePay in class C without instance is identical to calling `this.calculatePay();` which means that you are calling it using an object of C, "Remember `this` is always default object present in the class?" So `this` is working as the current object (of class C) for invoking the method calculatePay() – Raman Kumar Sharma Sep 06 '17 at 08:08
  • aah wonderful ,thanks i,v got all the clarification now thank you very much for your time – kobewarui Sep 06 '17 at 08:13
  • But as i mentioned, A case can be there when you override calculatePay in class C, then to call the method from class A you have to implement what I have suggested in my answer. Anyways I think you got your answer. Cheers. ;) – Raman Kumar Sharma Sep 06 '17 at 08:19
  • yes i got that too ,using super and overriding too, thanks :) – kobewarui Sep 06 '17 at 08:23