java继承方法

java继承方法,java,inheritance,Java,Inheritance,我有一个接口,有三个抽象方法 public interface ThreeDemoInter { public void a(); public void b(); public void c(); } 我有一个父类具有相同的三种方法: public class ParentThree { public void a(){ System.out.println("parent a"); } public void b(){

我有一个接口,有三个抽象方法

public interface ThreeDemoInter {
    public void a();
    public void b();
    public void c();
}
我有一个父类具有相同的三种方法:

public class ParentThree {

    public void a(){
        System.out.println("parent a");
    }
    public void b(){
        System.out.println("parent b");
    }
    public void c(){
        System.out.println("parent c");
    }
}
最后是一个子类,它有两个扩展父类和实现接口的方法

public class ChildThree extends ParentThree implements ThreeDemoInter {

    public void a(){
        System.out.println("child a");

    }
    public void b(){
        System.out.println("child b");
    }
}

我关心的是为什么没有得到方法c()的错误

我知道它扩展了具有此方法的父类,但其背后的概念并不完全清楚。 提前谢谢。正在查找此代码背后的解释

我关心的是为什么没有得到方法c()的错误

因为它的父类(ParentThree)已经为子类(ChildThree)所需的方法c()定义了实现

规则规定,任何实现接口的类都必须实现接口中声明的所有方法

当编译代码以检查实现依赖关系时,首先从父类到层次结构检查请求,从而解决实现问题。因此,父类已经满足了实现方法c()的请求

我关心的是为什么没有得到方法c()的错误

因为它的父类(ParentThree)已经为子类(ChildThree)所需的方法c()定义了实现

规则规定,任何实现接口的类都必须实现接口中声明的所有方法


当编译代码以检查实现依赖关系时,首先从父类到层次结构检查请求,从而解决实现问题。因此,要求实现方法c()已被父类满足。

这是因为ChildThree类扩展了类ParentThree,该类定义了所有三个方法,因此在继承该类后将继承这些方法,因此如果不必要,也无需再次定义这些方法。

这是因为ChildThree类扩展了类ParentThree定义了所有三个方法的人在继承该类后也会继承这些方法,因此如果不必要,无需再次定义这些方法。

正如我们所知,子类可以访问其所有父类方法。 我们只在必须自定义方法时重写。意味着它在access中有所有三个方法。这就是为什么不需要实现方法c()
希望,您明白了。

我们知道,子类可以访问其所有父类方法。 我们只在必须自定义方法时重写。意味着它在access中有所有三个方法。这就是为什么不需要实现方法c()
希望,你明白了。

因为在你的
父母三人中,你已经实现了这些方法。
根据继承,所有方法都可以在子类中访问。
因此,
ChildThree
中的所有相同方法都不是实现方法。但它们是重写方法。 就像在您的代码
child中一样,有三种
重写方法

public void a(){..}
public void b(){..}

因此,在您的
ChildThree
中,您没有实现
ThreeDemoInter
,而是覆盖了
ParentThree
的方法,这就是为什么您没有得到预期的错误。

因为在您的
ParentThree
中,您已经实现了这些方法。 根据继承,所有方法都可以在子类中访问。 因此,
ChildThree
中的所有相同方法都不是实现方法。但它们是重写方法。 就像在您的代码
child中一样,有三种
重写方法

public void a(){..}
public void b(){..}

所以,在您的
ChildThree
中,您没有实现
threedemoninter
,而是覆盖了
ParentThree
的方法,这就是为什么您没有得到预期的错误。

java中有一条核心规则,任何类实现任何接口,那么该类也应该实现其所有方法,除非该类是抽象的

现在,在您的例子中,有两个类
ParentThree
,即父类和
ChildThree

ChildThree
正在实现
ThreeDemoInter
接口,因此它应该实现其所有方法,并且它还扩展了ParentThree


ChildThree是
ParentThree
的子类,因此
ParentThree
的所有方法都可以在
ChildThree
类中访问,因此现在在
ChildThree
中,我们有了在
ParentThree
类上实现的所有方法a()、b()和c(),而
ChildThree
类只是覆盖了方法a()b()

java中有一条核心规则,任何类实现任何接口,那么该类也应该实现其所有方法,除非该类是抽象的

现在,在您的例子中,有两个类
ParentThree
,即父类和
ChildThree

ChildThree
正在实现
ThreeDemoInter
接口,因此它应该实现其所有方法,并且它还扩展了ParentThree


ChildThree是
ParentThree
的子类,因此
ParentThree
的所有方法都可以在
ChildThree
类中访问,因此现在在
ChildThree
中,我们有了在
ParentThree
类上实现的所有方法a()、b()和c(),而
ChildThree
类只是覆盖了方法a()和b()

如果您使用类加载器或通过反射列出
ChildThree
类的方法,它将列出三种方法
a
b
c
。由于它扩展了
ParentThree
类,这些方法可以作为自己的公共方法使用

接口定义实现它的类应该有三个方法
a
b
c

接口实现检查实现类上是否存在这些方法
ChildThree
通过了该测试,因此它是一个有效的实现。

What You Can Do in a Subclass A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members: The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
public class ChildThree extends ParentThree implements ThreeDemoInter{
//if no any method works fine
}