【发布时间】:2012-05-24 19:03:00
【问题描述】:
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();
}
}
我用一个抽象方法 show() 和一个具体方法 Display() 创建了一个抽象类超父类。现在我们创建一个父类,用一个抽象方法 detail() 和具体方法 display() 扩展父类,该方法从父类覆盖并在父类中实现抽象的 show() 方法,现在我创建了一个子类扩展父类,并在父类中实现抽象方法 Detail(),在父类和超父类中覆盖 display() 方法,并在父类和父类中覆盖 show()在父母中。现在我创建一个子实例并运行所有方法,它调用所有子方法,很好。如果我们想运行父方法,那么我们在构造函数中使用 super.parent 方法,运行很好。但是我如何运行超父方法 display()从儿童班。
【问题讨论】:
-
有什么不可能的,请您指定。
标签: java inheritance abstract-class