• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to invoke a grand-parent method?  RSS feed

 
John-Philippe Verger
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have 3 classes: Class1, Class2 and Class3. Class2 extends Class1 and Class3 extends Class2.Class1 has a method M( ) (M has no argument).Class2 overrides the method M it inherits from Class1, and Class3 overrides the method M it inherits from Class2. If I'm in Class2 and want to call Class1 version of method M, I write :

.If I'm in Class3 and want to call Class2 version of method M, I write :

. But how must I proceed if I'm in Class3 and need to call directly Class1 version of method M (in other terms , if from Class1 I want to call Class1 grand-parent version of method M ?).

Thanks you in advance for your answers.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot -- the language does not allow it. In general, if you feel this is necessary, there's probably something wrong with your design. Either Class3 should be extending Class1 directly, or there's something missing from Class2's API.

If you can modify Class2, you could add a method like



which you could then call from Class3. But generally this is just a workaround; a larger design change is probably needed.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot do this and maintain the conventions of object-oriented inheritance.

Object ---> Vehicle ---> Car ----> Limousine

If in the Limousine class you wish to call the Vehicle class version of the "stop" method, then your Limousine may not be behaving as a Car object any more. So you cannot write super.super.stop();





I won't tell you that you could write ((Vehicle)this).stop();. You didn't see that class-cast at all.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot. And if you have to, that means your class design is cumbersome. If you extend Class2 but want to use Class1 behavior, extend Class1 instead of Class2.
A Subclass augments to the behavior of the parent. Class2 overrides M(), therefore you inherit it and have to stick with it.

There is one trick though. You could chain the super calls, e.g. have a method in Class2 call super.M(). You can then call this method from Class3. It's a bad approach though.

EDIT: I hate being the last one to say the same thing ... ;)
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three posts all starting "You cannot" within 5 minutes
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great minds think alike!

(and us too apparently.)
 
John-Philippe Verger
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your answers.
 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling grand parent method is not part of java language specifications but with simple bytecode manipulations using ASM you can do it. Here is solution @ http://rohandhapodkar.blogspot.in/2012/03/call-grand-parent-method-in-java.html
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan Dhapodkar wrote:Calling grand parent method is not part of java language specifications but with simple bytecode manipulations using ASM you can do it. Here is solution @ http://rohandhapodkar.blogspot.in/2012/03/call-grand-parent-method-in-java.html

I would strongly advise against doing anything like that. The solution looks contrived and extra-lingular and is likely to be brittle (and the author himself admits that it has limitations).

@John-Philippe: Keep things simple and find another way to do what you need. As others have said, 'grandparenting' suggests that you haven't defined your classes correctly.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!