简体   繁体   English

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

[英]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.

相关问题 为什么不能扩展接口“泛型方法”并将其类型缩小到继承的接口“类通用”? - Why can't I extend an interface “generic method” and narrow its type to my inherited interface “class generic”? 为什么接口和抽象方法无法实例化? - why interface and abstract methods can't be instantiated? 为什么我们不能在没有匿名类方法的情况下在java中实例化接口或抽象类? - Why can't we instantiate an interface or an abstract class in java without an anonymous class method? Java:强制实现接口以扩展抽象类 - Java: Force implementation of interface to extend abstract class 当您可以扩展和实例化 class 时,为什么要制作它? - Why to make a class abstract when you can extend and instantiate it? 为什么我们不能在不修改“ public”的情况下实现从接口到抽象类的方法? - why we can't implement methods from interface to abstract class, without modifying “public”? 为什么Java中的许多Collection类都会扩展抽象类并实现接口? - Why do many Collection classes in Java extend the abstract class and implement the interface as well? 为什么使用抽象类而不是接口? - Why use abstract class and not interface? 为什么一个类不能扩展枚举? - Why can't a class extend an enum? 为什么enum不能在Java中扩展类? - Why can't an enum extend a class in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM