简体   繁体   English

为什么抽象方法必须由第一个具体的 class 来实现,而不是再往下走?

[英]Why do abstract methods have to be implemented by the first concrete class, and not one further down the chain?

I'm curious as to why abstract methods MUST be overridden by the first concrete implementing class, and not one further down the hierarchy change.我很好奇为什么抽象方法必须被第一个具体实现 class 覆盖,而不是在层次结构更改的更下方。

I'm not suggesting I want to do this, but I'm curious as to why it has to be the first class我并不是说我想这样做,但我很好奇为什么它必须是第一个 class

Consider this example考虑这个例子

abstract class Upper
{
    abstract void doSomething();
}

class Middle extends Upper
{
    void doSomething()
    {
       // I'm forced to be implemented here
    }
}

abstract class Lower extends Middle
{

}

class Bottom extends Lower
{
    void doSomething()
    {
        // I'm valid, but I'm too far down the hierarchy
    }
}

By definition a normal class must implement all abstract methods.根据定义,普通的 class 必须实现所有抽象方法。 If you would declare Middle abstract, then you would not have to implement the methods in Middle.如果您要声明 Middle 抽象,那么您将不必在 Middle 中实现方法。

A normal class can be instantiated, whereas a abstract class cannot.可以实例化普通的 class,而抽象的 class 不能。 Think about what would happen if you try to call the methods that are not implemented in the class.想想如果你尝试调用 class 中没有实现的方法会发生什么。

concrete classes are concrete.具体的类是具体的。
Which means they should be able to be initialized and used.这意味着它们应该能够被初始化和使用。
So, if you don't implement the methods, how would it be properly used?那么,如果你不实现这些方法,如何正确使用它呢? it will be incomplete and thus not concrete.这将是不完整的,因此不具体。

You could make another abstract class inherit an abstract class.您可以让另一个抽象 class 继承一个抽象 class。

Just make Middle an abstract class, and it's fine.只需将Middle设为抽象 class,就可以了。

If you want to keep Middle as a concrete class but also remove the doSomething from it, please explain what you'd want this code to do:如果您想将Middle保留为具体的 class 但还要从中删除doSomething ,请说明您希望此代码做什么:

Upper x = new Middle();
x.doSomething();

If Middle is a concrete class, an instantiated version that didn't define an abstract method wouldn't be callable (as it has no definition).如果 Middle 是具体的 class,则没有定义抽象方法的实例化版本将无法调用(因为它没有定义)。 This would inherently make Middle an abstract class as well.这本质上也会使 Middle 成为抽象的 class 。

Because you cannot have abstract methods in a class that is not abstract.因为在非抽象的 class 中不能有抽象方法。 If you don't want need any of the methods, you don't need to extend that class.如果您不需要任何方法,则不需要扩展该 class。

Instead, just have class Lower extend Upper .相反,只需让class Lower extend Upper

If you were to wish to still have Lower inherit both the abstract methods and inherit from Middle , simply declare Middle as abstract.如果您仍然希望Lower继承抽象方法和继承Middle ,只需将Middle声明为抽象。

You're forced to implement doSomething in Middle , because Middle is a concrete class.你不得不在Middle中实现doSomething ,因为 Middle 是一个具体的 class。 That means that Middle must provide an implementation of all abstract methods from its superclass as well as an implementation of any interface methods from interfaces that it implements.这意味着Middle必须提供来自其超类的所有抽象方法的实现,以及来自它实现的接口的任何接口方法的实现。

If you marked Middle as an abstract class, you can delegate the implementation of doSomething to Bottom .如果将Middle标记为抽象 class,则可以将doSomething的实现委托给Bottom

For more information about abstract classes, see this link from theJava Language Specification .有关抽象类的更多信息,请参阅Java 语言规范中的此链接。

In your example without the declaration of doSomething in Middle there'd be no code to run.在您的示例中,如果没有在 Middle 中声明 doSomething,则不会运行任何代码。 Therefore the class would have to be abstract.因此 class 必须是抽象的。

It isn't really a question of must .这实际上不是must的问题。 An abstract class is just a class with one or more methods without implementations.抽象的 class 只是一个 class 具有一个或多个方法而没有实现。

It's more like saying a biological human male must have a Y chromosome.这更像是说一个生物学上的人类男性必须有一条 Y 染色体。 It isn't a law;这不是法律; it's just part of the definition.它只是定义的一部分。

In your example, if you don't want to implement doSomething in Middle , then you really don't want Middle to be concrete .在您的示例中,如果您不想在Middle实现doSomething ,那么您真的不希望Middle变得具体 That is, it would have a method that has no definition.也就是说,它将有一个没有定义的方法。 So it's abstract.所以很抽象。

You could, of course, implement it as an empty function that does nothing.当然,您可以将它实现为一个什么都不做的空 function。 That's pretty close to what you're asking about, I believe.我相信这与您要问的非常接近。

Really it's just a question of terminology.真的,这只是术语的问题。 If there are unimplemented members, then the type's abstract.如果有未实现的成员,则为类型的抽象。 It's that simple.就是这么简单。

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

相关问题 为什么EnumSet是作为抽象类实现的,而EnumMap是作为具体类实现的? - Why EnumSet is implemented as abstract class and EnumMap is implemented as concrete class? 抽象 class 应该同时具有抽象方法和具体方法的解决方法 - An abstract class should have both abstract and concrete methods workaround 为什么我们将类声明为抽象类,其中只有具体的方法? - Why we are declaring the class as abstract class it has only concrete methods in it? 在具体类中实现抽象方法 - Implementing abstract methods in a concrete class 所有具体方法的抽象类 - Abstract class with all concrete methods 为什么具体类的子类可以声明为abstract,而某些父类方法可以被重写并声明为abstract? - Why a subclass of the concrete class can be declared abstract, and some of the parent class methods can be overridden and declared abstract? 从抽象 class 调用方法,其中具体子类具有这些方法的不同输入参数 - Calling methods from abstract class where concrete subclasses have different input parameters of those methods 使用JMockit测试抽象类的具体方法 - Use JMockit to test the concrete methods of an abstract class 不覆盖抽象类中的抽象方法的具体子类 - Concrete subclass that does not override abstract methods from abstract class 抽象类和方法,为什么呢? - Abstract class and methods, Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM