aspose file tools*
The moose likes Beginning Java and the fly likes how to invoke a method a few levels higher in the inheritance tree? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to invoke a method a few levels higher in the inheritance tree?" Watch "how to invoke a method a few levels higher in the inheritance tree?" New topic
Author

how to invoke a method a few levels higher in the inheritance tree?

kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
if class C extends class B
and
class B extends class A
and
class A has a public method called add() which is overridden in both child classes B and C.How can the version A of method add() be invoked from class C?
thanks
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629



Try to write down your own small code because of that you understood the concept easily.


Life is easy because we write the source code.....
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
thanks
but this is not what I meant.how to invoke version A of add() from class C without creating an instance of A?
say for example if I want to have a method in C that has a line " System.out.print(/*A s method add()*/);"
what Im asking is what should come in between /*...*/?
thanks
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629



do you mean like this?
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
not exactly.(fine your code invokes A s add() within C.)
simply what I want to know is that whether there is a way to call a method in a super super class directly from a child class?
thanks
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Then in that case I think it will not possible.
kevinn lee
Ranch Hand

Joined: Feb 15, 2010
Posts: 87
thanks a lot
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629


thanks a lot


You are Welcome


Hello ranchers please correct me if I am wrong.
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
It is quite possible and I won't tell you how to do that. The reason is that a subclass "is-a" superclass; if there is a method foo() which does something in the superclass, and something different again in the superclass of that superclass, then calling super.super.foo() breaches the principle that a class behaves as if it "is-a" superclass. (and the compiler definitely won't like super.super).

So I won't tell you how it is done, just as I wouldn't tell somebody else here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

But super.add() will never allow you to go up more than one class. So from B you can call super.add() to call A's add() method, but from C super.add() calls B's add() method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31054
    
162

What are you actually trying to accomplish? It seems like a refactoring to a better design would be better here. For example,

A:
add()
addAnA()

B:
add()

C:
add() - can call addAnA() directly without jumping through any hoops


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to invoke a method a few levels higher in the inheritance tree?