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()创建一个抽象类superparent。现在我们创建一个Parent类扩展superparent,用一个抽象方法detail()和具体方法display(),它是从superparent重写的,并实现superparent中抽象的show()方法,现在我创建一个子类扩展Parent,实现Parent中抽象方法Detail(),覆盖父类和父类中的display()方法,并覆盖父类中的show()方法。现在我创建一个子实例并运行所有方法,它调用所有子方法,很好。如果我们想运行父方法,那么我们在构造函数中使用 super.parent 方法,运行良好。但是我如何从子类运行 super 父方法 display() 。
最佳答案
Java 语言不支持此功能。
您必须从 Parent
调用 SuperParent.show()
并从 Child
调用此代码:
abstract class Parent extends SuperParent {
...
public void superParentShow() {
super.Show();
}
}
然后调用
super.superParentShow()
来自子
。
相关问题:
关于java - 我如何使用子类中的父类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10631720/