This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a method in the supermost class

 
vijay kumarg
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this inheritance hierarchy,
[ class B extends class A and
class C extends class B ]
if a method foo() declared in A and has been overridden in B and C,
then how to call directly the version of method foo() in class A from class C.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
supermost.foo()

sorry I could not resist
 
Bhavik patel
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you can not.....but im not that sure.....this has been discussed in head first....
 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not possible to directly call the method of class A in class C, using class C reference, even if it implements polymorphism. But it is always possible to create a reference of class A in class C and use it to call the supermost class method foo().
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, if you feel the need to do that, you probably have a deeper problem with your design. Can you give us an example of where you would want to do that?
 
Jitendra Jha
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Preuss"
Basically, if you feel the need to do that, you probably have a deeper problem with your design. Can you give us an example of where you would want to do that?



it does not matter where to do that!
Actually what i am interested in knowing is if it is possible to do it?
Actually this problem has been troubling me too.
kindly help us with it!!
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Ilja means is what are the classes you have where you need to call the supermost class method?
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jitendra Jha:
it does not matter where to do that!
Actually what i am interested in knowing is if it is possible to do it?
No, you can't do it. Ilja's point was that if you have a real design where you really need to do it, then you have a problem in your design. If you have a theoretical question then the answer is No.
 
Remko Strating
Ranch Hand
Posts: 893
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think is bad practice to do so, but you can make the following construct



Now I'm calling a method from Class A with the use of Class C.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say you are calling C's method. What that method happens to do is call B's method, which then call's A's method.

You are CALLING A's method(), but you are not directly calling it.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic