wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to call a method in the supermost class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to call a method in the supermost class" Watch "How to call a method in the supermost class" New topic
Author

How to call a method in the supermost class

vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

supermost.foo()

sorry I could not resist


[My Blog]
All roads lead to JavaRanch
Bhavik patel
Ranch Hand

Joined: Feb 20, 2007
Posts: 49
i think you can not.....but im not that sure.....this has been discussed in head first....


Bhavik Patel
Glassfish Consultancy service At http://www.jmatrix.in
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
"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!!


Jitendra
SCJP1.5
SCWCD1.5
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think what Ilja means is what are the classes you have where you need to call the supermost class method?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a method in the supermost class
 
Similar Threads
how to call parent class data
What is right syntax to bypass virtual method call?
How do u call Grandparent's foo
BridgeMethod Overriding Rules
super.super