1
abstract class SuperParent
{
    public abstract void Show();
    public void Display()
    {
        System.out.println("HI............I m ur grandpa and in Display()");
    }
}

abstract class Parent extends SuperParent
{
    public abstract void Detail(); 
    public void  Show()
    { 
        System.out.println("implemented  abstract Show()method of Superparent in parent thru super");
    }
    public void Display()
    {
        System.out.println("Override display() method of Superparent in parent thru super");    
    }
}

public class Child extends Parent
{
    Child()
    {
        super.Show();
        super.Display();
    }
    public void  Show()
    {
        System.out.println("Override show() method of parent in Child");
    }
    public  void Detail()
    {
        System.out.println("implemented abstract Detail()method of parent ");
    }
    public void Display()
    {
        System.out.println("Override display() method of Superparent and Parent in child ");    
    }

    public static void main(String[] args) {
        Child c1= new Child();
        c1.Show();
        c1.Display();

        Parent p1=new Child();
        p1.Detail();
        p1.Display();
        p1.Show();

    }
}

I create a abstract class superparent with one abstract method show() and one concrete method Display().Now we create a Parent class extends superparent with one abstract method detail()and concrete method display() which is override from superparent and implement show() method which is abstract in superparent ,now i create a child class extends Parent,with implement method Detail() which is abstract in Parent and overide display() method which is in parent and superparent and overide show() which is in parent. now i create a instance of child and run all method and it call all child method,fine.and if we want to run parent method then we use super.parent method in constructor,run fine .but how i run superparent method display() from child class.

1 Answers1

1

This is not supported by the Java language.

You'll have to call SuperParent.show() from Parent and invoke this code from Child:

abstract class Parent extends SuperParent {

    ...

    public void superParentShow() {
        super.Show();
    }
}

and then call

super.superParentShow()

from Child.

Related question:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • if we use a heirarchy of abstarct classes and override methods then we cann't do code for every method of superparent.its worthless..and not feasible – Rahul Jaiswal May 17 '12 at 10:13
  • 1
    Yes indeed. But you're not supposed to do that. If the Child class extends Parent, then it IS a Parent, and if Parent is meant to be subclasses, then it should provide the necessary methods for subclasses, but not more. Providing every method from the grandparent is the best way to break encapsulation. – JB Nizet May 17 '12 at 10:18
  • we know that super is use for executing parent method and constructor means it have the reference of parent class if we type cast the super with superparent then we may acces the method of superparent.Can we cast the super – Rahul Jaiswal May 17 '12 at 11:43
  • The reference to the parent (and superparent) is the same as the child reference. ('this'). You can cast but it will not make a difference dince all methods are virtual. – aioobe May 17 '12 at 12:03