• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to invoke a grand-parent method?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79973
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79973
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three posts all starting "You cannot" within 5 minutes
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your answers.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic