• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try to write down your own small code because of that you understood the concept easily.
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


do you mean like this?
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then in that case I think it will not possible.
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks a lot


You are Welcome


Hello ranchers please correct me if I am wrong.
Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49363
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34651
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic