Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call base class method

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A { public void perform_work(){} }
class B extends A { public void perform_work(){} }
class C extends B { public void perform_work(){} }

can we let perform_work() method of A to be called from an instance method in C?
please explain
 
T George
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure as I am just learining but
if you creat an object of A then you should be able to call the method in A.


please correct me if I am wrong
Thomas
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can we let perform_work() method of A to be called from an instance method in C?


If you create an instance of class C then you can't call the perform_work() method of class A. Even the perform_work method in C can't call the perform_work method of class A.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit Garg:


If you create an instance of class C then you can't call the perform_work() method of class A. Even the perform_work method in C can't call the perform_work method of class A.


As I understood it, dynamic (late binding) calls the method most local to the object at runtime. So in this case the JVM will see if there is a method perform_work() in Class C, and run that, if not it will go to the parent class (class B) and if not there Class A.

If you wish to call the parent class method, I believe you should use the super keyword so: Class C { super.perform_work()} which forces the JVM to seek and use the method in class B. If for some reason you wish to use class A perform_work (but I am unsure as to why, if you can inherit it anyway) I am not completeley sure if you can say super.super (I dont htink this is possible), but you could call super in C and then in B. But again, why would you if you are able to override the method in class C.

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Davies:


As I understood it, dynamic (late binding) calls the method most local to the object at runtime. So in this case the JVM will see if there is a method perform_work() in Class C, and run that, if not it will go to the parent class (class B) and if not there Class A.

If you wish to call the parent class method, I believe you should use the super keyword so: Class C { super.perform_work()} which forces the JVM to seek and use the method in class B. If for some reason you wish to use class A perform_work (but I am unsure as to why, if you can inherit it anyway) I am not completeley sure if you can say super.super (I dont htink this is possible), but you could call super in C and then in B. But again, why would you if you are able to override the method in class C.



Doh! silly me, scrap this super is reserved only for calling constructors!
 
T George
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super.perform_work does work from within the subclass' overriden method
but super.super.perform_work does not look like it works

Thomas
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok if you wish to call the method from class A. You must create an instance of class A in class C and call the method on that instance. I have complied and run the following code which works fine.

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..which gives the output:

From Class C
**********
From Class B
**********
From Class A
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Davies:

Doh! silly me, scrap this super is reserved only for calling constructors!

No it isn't. You can also use it to call a method from your super class. You're right however that you can only go back one level. You can't call super.super.perform_work.

And you also shouldn't, because it goes against the principles of object orientation. If you want to inherit class A's behaviour and not class B's, you should extend class B but A instead. If you want a combination of both, that's not directly possible.
[ August 11, 2008: Message edited by: Rob Prime ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see Rob has beaten me to it.

This comes up occasionally, so a search might show previous threads.
Please don't say "base class" or "parent class" say "superclass," which is the correct term used in Java. If you hand in a report of your work in Java with "base class" in you may lose marks.

The idea of inheritance is that the subclass object "is a" superclass object. So your C "is a" B. That means that whatever happens in B happens in C, unless it is overridden.
Similarly your B "is an" A. That means that whatever happens in A happens in B, unless it is overridden.

What would happen if you called an "A" method from "C?" You would be bypassing the method in "B" and you could then no longer say your "C" "is a" B. That is what Rob means about breaking the rules of object orientation, and why it is not possible to call an "A" method directly from "C".
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good example, but of course, one (as you have suggested) could no doubt say

Class C extends A {} ..

Does it not follow that if a B is an A and a C is a B then A C is an A?

 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does. You do lose the methods and behaviour inherited from B though. So the choice is clear:
  • extend A and lose the inherited methods and behaviour from B, which you'll need to reimplement if you need it
  • keep extending B and accept the behaviour you inherit from B
  •  
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49396
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stephen Davies:
    Good example, but of course, one (as you have suggested) could no doubt say

    Class C extends A {} ..

    Does it not follow that if a B is an A and a C is a B then A C is an A?

    Yes, but if you try to get directly from C to A with super.super, then C no longer "is a" B.

    If you do write super.super I don't think it will compile in the first place.
     
    T George
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a small question about this



    when C inherits B does it have the knowledge about the fact that m() in B is an overriden copy of m() in A
    I am not sure about this but shouldn't B look like it has two copies of m() when it overrides m() one that it inherited and second the overriden one?
    I am not clear about this?
    Can someone please clarify?
    Thomas
     
    Amit Ghorpade
    Bartender
    Posts: 2854
    10
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    when C inherits B does it have the knowledge about the fact that m() in B is an overriden copy of m() in A

    No C does not know that there is a method in A called m().There is no way to know that.

    shouldn't B look like it has two copies of m() when it overrides m() one that it inherited and second the overriden one?

    No there is only one method which is both inherited and overridden at the same time.

    Hope this helps
     
    Amit Ghorpade
    Bartender
    Posts: 2854
    10
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And I think that the term "overridden copy" is confusing you.
    There isn't a copy.
    The proper term is overridden version.

    Hope this helps
     
    Arijit Daripa
    Ranch Hand
    Posts: 142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stephen Davies:


    I am not completeley sure if you can say super.super



    We can not call super.super
     
    T George
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thnx for the help Amit
    Thomas
     
    Amit Ghorpade
    Bartender
    Posts: 2854
    10
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are welcome Thomas
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic