File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes call base class method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "call base class method" Watch "call base class method" New topic
Author

call base class method

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
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


SCJP1.5
T George
Greenhorn

Joined: Jun 26, 2008
Posts: 9
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

Joined: Aug 03, 2008
Posts: 9280
    
  17


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.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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.



be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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

Joined: Jun 26, 2008
Posts: 9
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

Joined: Jul 23, 2008
Posts: 352
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

Joined: Jul 23, 2008
Posts: 352
..which gives the output:

From Class C
**********
From Class B
**********
From Class A
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Jul 23, 2008
Posts: 352
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

    Joined: Oct 13, 2005
    Posts: 36508
        
      16
    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

    Joined: Jun 26, 2008
    Posts: 9
    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

    Joined: Jun 06, 2007
    Posts: 2712
        
        5

    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


    SCJP, SCWCD.
    |Asking Good Questions|
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2712
        
        5

    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

    Joined: Aug 09, 2008
    Posts: 142
    Originally posted by Stephen Davies:


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



    We can not call super.super


    SCJP 5
    T George
    Greenhorn

    Joined: Jun 26, 2008
    Posts: 9
    thnx for the help Amit
    Thomas
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2712
        
        5

    You are welcome Thomas
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: call base class method
     
    Similar Threads
    Mock question
    where should i call method main
    Multiple inheritance
    how to using the methods of superclass?
    Code call (Inheritance)