java 为什么抽象类不能扩展接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25214075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:35:31  来源:igfitidea点击:

Why can't an abstract class extend an interface?

javainterfaceabstractextends

提问by Praveen Reddy Katta

I was just wondering why an abstractclass can't extend an interface.

我只是想知道为什么一个abstract类不能扩展interface.

Since we can't instantiate an abstractclass, can't I just extend the interface and then override those methods in the classes which are extending the abstract class?

既然我们不能实例化一个abstract类,难道我不能只扩展接口,然后覆盖扩展抽象类的类中的那些方法吗?

For example

例如

abstract class AbstractClass extends InterfaceA 
{

}

interface InterfaceA
{
   public void methodToBeImplemented();
}

class MyClass extends AbstractClass
{
  @Override
  public void methodToBeImplemented(){
      //do something
  }
}  

回答by Qix - MONICA WAS MISTREATED

Because you don't extendsan interface- you implementsit.

因为你不是extends一个接口- 你implements它。

interface InterfaceA
{
   public void methodToBeImplemented();
}

abstract class AbstractClass implements InterfaceA 
{

}

class MyClass extends AbstractClass
{
  @Override
  public void methodToBeImplemented(){
      //do something
  }
}  

An abstract class is still a class, just like any other. Only interfaces can extendsother interfaces.

抽象类仍然是一个类,就像任何其他类一样。只有接口可以使用extends其他接口。

回答by Nick

You can actually extend interfaces in Java, but it would still be called an interface.

您实际上可以在 Java 中扩展接口,但它仍将被称为接口。

Then you can use this extended interface implemented in your abstract class.

然后您可以使用在您的抽象类中实现的这个扩展接口。

interface InterfaceName
{
    public void foo();
}

public interface ExtendedInterface extends InterfaceName
{
    public void bar();
}

public class ClassName implements ExtendedInteface 
{
    //implementation for all methods in InterfaceName and ExtendedInteface
    ...
}

Praveen, its just the way the designers designed it. It goes back to the philosophy of Java and the way classes and interfaces are meant to be used. Lets say you have following classes: Student, Professor, Employee, Technician. And following interfaces: Demography, Paydetails and Personaldetails.

Praveen,这正是设计师设计它的方式。它可以追溯到 Java 的哲学以及类和接口的使用方式。假设您有以下课程:学生、教授、员工、技术员。以及以下界面:人口统计、Paydetails 和 Personaldetails。

Ideally, classes professor and technician can be both extended from the class employee and it makes sense. However, imagine extending class Technician from Demography or Personaldetails. It doesnt make sense. Its like extending class sky from class blue. Just cause you can do doesnt mean you should do it. However, it makes more sense to have a class technician extend from employee and implement interfaces like Paydetails or demography. Its like extending sky from nature but implementing interface blue (giving it function capabilities of blue).

理想情况下,班级教授和技术员都可以从班级员工中扩展,这是有道理的。但是,想象一下从人口统计或个人详细信息扩展类技术员。这没有意义。它就像从类蓝色扩展类天空。仅仅因为你可以做并不意味着你应该这样做。但是,让班级技术人员从员工扩展并实施 Paydetails 或人口统计等界面更有意义。它就像从自然中延伸天空,但实现了蓝色接口(赋予它蓝色的功能)。

This is the basic reason why Java developers were against multiple inheritance (unlike cpp) and instead use interfaces. In your question, it doesnt matter if the class is abstract or not. You simple cant extend from an interface cause it does not make sense logically.

这是 Java 开发人员反对多重继承(与 cpp 不同)而使用接口的基本原因。在您的问题中,该类是否抽象并不重要。您不能简单地从接口扩展,因为它在逻辑上没有意义。

回答by anjli

in interface extends only interface .when we use a interface in a class then it need to implements in class.and another thing is abstract class contain a both abstract and non abstract method while interface contain only abstract method that are define where implementation of inteface(subclass).

在接口中只扩展接口。当我们在类中使用接口时,它需要在类中实现。另一件事是抽象类包含抽象和非抽象方法,而接口仅包含定义接口实现位置的抽象方法(子类)。