简体   繁体   English

是不是必须强制在Child抽象类中实现所有接口方法,而必须强制在Grand Child类中实现所有接口方法?

[英]Y it's not mendatory to implement all interface methods in Child abstract class But Mendatory to implement all Interface methods in Grand Child class?

I made an interface as: 我将接口设置为:

interface Castle
    {
    public void sad();
    public void cool();
    }

Then i made a child abstract class of it as: 然后我做了一个孩子的抽象类为:

abstract class Castle2 implements Castle
    {
    abstract void sad();
    }

Here I left the implementation of cool(), and if i complile the above code, it compiled Successfully But when i added 1 more sub class of Castle2 as: 在这里,我离开了cool()的实现,如果我遵守了上面的代码,则编译成功,但是当我添加了Castle2的另一个子类时:

class Castle3 extends Castle2{
public void sad(){
System.out.println("SAD");
}
public static void main(String...args){
new Castle3().sad();
}
}

And tried to compile the above code then it is not even compiling my code stating the following error 并尝试编译上面的代码,那么它甚至没有编译我的代码,说明以下错误

Castle.java:13: error: Castle3 is not abstract and does not override abstract method cool() in Castle Castle.java:13:错误:Castle3不是抽象的,并且不覆盖Castle中的抽象方法cool()

When i run javap tool on class Castle2, then i got the following result 当我在Castle2类上运行javap工具时,得到以下结果

abstract class Castle2 implements Castle {
Castle2();
public void sad();
}

Why Compiler is Forcing me to implement a interface Castle's method in class Castle3 which is not even present in class Castle2? 为什么Compiler强迫我在Castle3类中实现一个Castle3方法的接口,而Castle2类中却没有该方法? And Why Compiler is not Forcing me to implement a interface Castle's method in class Castle2? 为什么编译器不强迫我在Castle2类中实现接口Castle的方法?

Because we cannot create object of abstract class. 因为我们不能创建抽象类的对象。 But if abstract class has a subclass, this subclass can be instantiated. 但是,如果抽象类具有子类,则可以实例化该子类。 Thats why we need implement all methods in subclass 这就是为什么我们需要在子类中实现所有方法

That is because a concrete class must have implementation because they can be instantiated. 那是因为具体的类必须具有实现,因为它们可以被实例化。 Suppose they allow concrete class not to implement all the methods of an interface, there will arise a problem. 假设它们允许具体类不实现接口的所有方法,则会出现问题。 If in the code we call the unimplemented method, JVM wont be having the address of the unimplemented method. 如果在代码中我们调用未实现的方法,则JVM将不会具有未实现的方法的地址。

But abstract classes can not be instantiated. 但是不能实例化抽象类。 Thats why it is not mandatory to implement methods of an interface by an abstract class. 这就是为什么通过抽象类实现接口的方法不是强制性的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:在实现接口的子类中,如何仅实现和引用子类的方法和类? - Java: In child classes that implement interface, how to implement and refer to child class only methods and classes? 在Java中继承抽象类时是否需要再次实现接口的所有方法? - Do i need to implement all the methods of interface again while inheriting abstract class in Java? class中的接口方法没有实现吗? - Interface methods in a class that does not implement it? 接口的所有方法都是抽象的吗? - Are all methods of interface abstract? 具有所有抽象方法和接口的抽象类之间的区别是什么? - What is difference between abstract class with all abstract methods and interface(not technically) 强制客户端类实现子类的接口? - Forcing a client class to implement a child class' interface? 使用反射获取接口或抽象 class 的所有方法 - Get all methods of an Interface or Abstract class using Reflection 删除一个抽象类/接口并批量更改所有继承/实现它的类 - Delete an abstract class/interface and change all classes that inherit/implement it in bulk 如何实现一个没有定义所有抽象方法的类? - How to implement a class that doesn't define all the abstract methods? 当我们在类中定义接口的抽象方法时,我们是“覆盖”或“实现”还是简单地说“定义”那些方法? - When we define the abstract methods of an interface in a class do we 'override' or 'implement' or simply say 'define' those methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM