aspose file tools*
The moose likes Beginning Java and the fly likes Calling Supermost class's method with sub most's class reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling Supermost class Watch "Calling Supermost class New topic
Author

Calling Supermost class's method with sub most's class reference

Deepak Sahu
Greenhorn

Joined: Dec 08, 2009
Posts: 15
Hi All,

Can anyone give the solution for this scenario.

class A
{
public void go()
{
System.out.println("Inside A");
}
}

class B extends A
{
public void go()
{
System.out.println("Inside B");
}
}

class C extends B
{
public void go()
{
System.out.println("InsideC");
}


public static void main(String args[])
{
C c1= new C();




}
}

The requirement is how do we call go() of class A with the reference of C


Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Not able to do this.
You can only call your immediate super class in the hierarchy. No 'skipping' of generations is allowed.

So my solution if you don't want C to invoke B's copy of the method would be to have C extend A directly, rather than B.



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3379
    
    9
Stefan Evans wrote:Not able to do this.
You can only call your immediate super class in the hierarchy. No 'skipping' of generations is allowed.

So my solution if you don't want C to invoke B's copy of the method would be to have C extend A directly, rather than B.


If you think about it, not even this. From a client point of view, there's no way to call B's method through a reference to a C instance. And making a subclass just to call super is rather pointless.

To the original poster: There's no way to do this. It defeats the concept of polymorphism. Why do you want to do this?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Also, please use the 'code' tags when posting java code. you can click the little buttons above where you enter your post to have them drop in, then paste your source between. This will preserver your formatting, making the code much easier to read.

I was going to edit your post to put them in, but found you don't have your code properly formatted. so, my next suggestion would be to ask you to please properly format your code, to make it easier to read.


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: Calling Supermost class's method with sub most's class reference
 
Similar Threads
how to invoke a method a few levels higher in the inheritance tree?
How to call "super.super.methodName()"
redefining inherited fuctions, what gets called then ?
What will this program print out? Why?
subclass calls the method of double superclass?