[英]Why can't an abstract class extend an interface?
I was just wondering why an abstract
class can't extend an interface
. 我只是想知道为什么
abstract
类不能扩展interface
。
Since we can't instantiate an abstract
class, 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
}
}
Because you don't extends
an interface - you implements
it. 因为您不
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
extends
other interfaces. 只有接口可以
extends
其他接口。
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. Praveen,这只是设计师设计的方式。 It goes back to the philosophy of Java and the way classes and interfaces are meant to be used.
它可以追溯到Java的哲学以及类和接口的使用方式。 Lets say you have following classes: Student, Professor, Employee, Technician.
假设您有以下课程:学生,教授,员工,技术人员。 And following interfaces: Demography, Paydetails and 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.
但是,请想象从人口统计学或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.
但是,让班级技术人员从员工中扩展并实现诸如Paydetails或人口统计学之类的接口更有意义。 Its like extending sky from nature but implementing interface blue (giving it function capabilities of blue).
就像从大自然中延伸出天空,但实现了蓝色的界面(赋予它蓝色的功能)。
This is the basic reason why Java developers were against multiple inheritance (unlike cpp) and instead use interfaces. 这是Java开发人员反对多重继承(与cpp不同)而使用接口的基本原因。 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.
您不能简单地从接口扩展,因为从逻辑上讲它没有意义。
in接口只扩展接口。当我们在类中使用接口时,它需要在类中实现。另一件事是抽象类同时包含抽象和非抽象方法,而接口仅包含定义inteface()实现位置的抽象方法。子类)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.