• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling super-to-superclass members

 
saxena vicky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I call the method of Class A1 from C1


public class A1 {

public void method()
{
System.out.println(" A1 ");
}
}

public class B1 extends A1{
public void method()
{
System.out.println(" B1");
}
}

public class C1 extends B1{
public void method()
{
super.method();// This will call the method of B1 class
//How can I call the method of Class A1 from C1
System.out.println(" C1");
}
}

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way you can directly refer to super-super class's instance method if both the super and super-super class of the sub class have instance method with same signature...
But still there is way you can manage it in your code..better if you figure it out yourself...keep trying, keep thinkin ...

saxena vicky wrote:How can I call the method of Class A1 from C1


public class A1 {

public void method()
{
System.out.println(" A1 ");
}
}

public class B1 extends A1{
public void method()
{
System.out.println(" B1");
}
}

public class C1 extends B1{
public void method()
{
super.method();// This will call the method of B1 class
//How can I call the method of Class A1 from C1
System.out.println(" C1");
}
}

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You asked a new question on this old thread. I don't think it is a good idea to add new questions to old thread. Fortunately, we have a new technique for removing new posts from threads, which I am trying out. I've never used it before. Your posts are now here.
 
saxena vicky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply swaraj.

But please tell me how can I call the overridden method of Class A1 from the method in Class C1
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. And if you think you need to do that, you're grossly misusing inheritance.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class can only call it's super class methods. Nothing above can be called (except for static methods of course).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic