Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super . super

 
sarada konda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
public void method1()
{
System.out.println("A"):
}
}

class B extends A
{
public void method1()
{
System.out.println("B");
}
}
class C extends B
{
public void method1()
{
super.method1();// but here i want call class A's method1
}
}

thanks in advance
 
Jef Cumps
cowbird
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In Java, you're not allowed to call super.super. Childs have acces to their parents, but not to their grandparents...

Why would you need to call a method in the grandparent-class? Maybe this problem points you to e design that could be better? If C needs the implementation of A, couldn't it be a direct child of A then?
 
mohan gavande
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi SARADA,
This might be helpful to you


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

class B extends A
{
public void method1()
{
A a1;
a1 = new B();
super.method1();
System.out.println("B");
}
}

class C extends B
{
public void method1()
{
super.method1();// but here i want call class A's method1
}
}

class GrandCall
{
public static void main( String str[] )
{
A a2 = new A();
A b1 = new B();
A c1 = new C();

c1.method1();
}
}

If u want to print only A then right code in B's method
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic