Abstract class extends concrete class

Heisenberg

I earlier learned that abstract class can extend concrete class. Though I don't see the reason for it from JAVA designers, but it is ok. I also learned that abstract class that extends concrete class can make overriden methods abstract. Why? Can you provide with use case where it is useful? I am trying to learn design patterns and I do not want to miss anything.

Here is example:

public class Foo 
{
    public void test()
    {
    }
}

public abstract class Bar extends Foo
{
   @Override
   public abstract void test();
}
Michael Berry

This becomes useful if I have a set of classes that I want a default implementation of test() for (so they can extend from Foo), and a subset of those classes that I want to force to provide their own implementation (in which case making it abstract in the subclass would enforce this.)

Of course, the alternative way in this example would be to declare test() abstract in the top level class rather than in the subclass, and that's what you'd usually do - but there are cases where satisfying the is-a relationship of inheritance means that it occasionally makes more sense from a design perspective doing it this way around. It's rare, but you do sometimes see it.

As an aside, though a special case, remember that all classes implicitly extend Object unless otherwise specified. So if you include this case, abstract classes extending concrete classes isn't so unusual after all!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何扩展 class<T extends class>

来自分类Dev

Android abstract parcelable class

来自分类Dev

Doctrine Extension Translatable with extends class

来自分类Dev

casting to an abstract generic base class

来自分类Dev

c# Factory for concrete implementation of generic base class

来自分类Dev

无法从Abstract <Generic>转换为Concrete:Abstract <Concrete:Generic>

来自分类Dev

VBA:是否有类似Abstract Class的东西?

来自分类Dev

Model Many to Many relations with Abstract Class

来自分类Dev

Abstract class inheriting a generic type requiring a constructor

来自分类Dev

passing Collection of abstract class as parameter in function

来自分类Dev

Is it possible in typescript to have a function return the type of a class that extends it

来自分类Dev

Cannot resolve method super in class which extends ArrayAdapter

来自分类Dev

JavaScript ES6 import/export and class extends

来自分类Dev

类型 RestTemplateBuilder 中的 requestFactory(Class<? extends ClientHttpRequestFactory>) 不适用

来自分类Dev

Java: Creating Reference Type from Abstract class and interface

来自分类Dev

Class needs to be abstract error while using traits in scala

来自分类Dev

Can I make virtual abstract class throw an exception

来自分类Dev

Why do I need another constructor in an extended abstract class?

来自分类Dev

使用Object.assign和class / extends有什么区别?

来自分类Dev

在 Karma 测试期间,“无法在‘文档’上执行‘querySelector’:‘class MyComponent extends HTMLElement”

来自分类Dev

如何克服 Node.js 中的“TypeError: Class extends value undefined is not a constructor or null”错误?

来自分类Dev

这些是什么以及它们为什么编译:“class Tst<T extends I1, I2, I3>”和“class Tst2<T extends Cls & I1, I2, I3> {}”?

来自分类Dev

为什么我可以在Python中实例化我的Abstract Base Class?

来自分类Dev

为什么使用非空构造函数的Trait扩展Abstract Class进行编译?

来自分类Dev

DBIx :: Class / SQL :: Abstract将SQL函数应用于WHERE子句中的列

来自分类Dev

为什么我可以在Python中实例化我的Abstract Base Class?

来自分类Dev

.class> .class和.class .class之间的区别

来自分类Dev

“ .class”和“ .class,.class .class”之间的区别?

来自分类Dev

在Java中将对象扩展抽象类的方法添加到类型为Abstract Class的Arraylist中的方法

Related 相关文章

  1. 1

    如何扩展 class<T extends class>

  2. 2

    Android abstract parcelable class

  3. 3

    Doctrine Extension Translatable with extends class

  4. 4

    casting to an abstract generic base class

  5. 5

    c# Factory for concrete implementation of generic base class

  6. 6

    无法从Abstract <Generic>转换为Concrete:Abstract <Concrete:Generic>

  7. 7

    VBA:是否有类似Abstract Class的东西?

  8. 8

    Model Many to Many relations with Abstract Class

  9. 9

    Abstract class inheriting a generic type requiring a constructor

  10. 10

    passing Collection of abstract class as parameter in function

  11. 11

    Is it possible in typescript to have a function return the type of a class that extends it

  12. 12

    Cannot resolve method super in class which extends ArrayAdapter

  13. 13

    JavaScript ES6 import/export and class extends

  14. 14

    类型 RestTemplateBuilder 中的 requestFactory(Class<? extends ClientHttpRequestFactory>) 不适用

  15. 15

    Java: Creating Reference Type from Abstract class and interface

  16. 16

    Class needs to be abstract error while using traits in scala

  17. 17

    Can I make virtual abstract class throw an exception

  18. 18

    Why do I need another constructor in an extended abstract class?

  19. 19

    使用Object.assign和class / extends有什么区别?

  20. 20

    在 Karma 测试期间,“无法在‘文档’上执行‘querySelector’:‘class MyComponent extends HTMLElement”

  21. 21

    如何克服 Node.js 中的“TypeError: Class extends value undefined is not a constructor or null”错误?

  22. 22

    这些是什么以及它们为什么编译:“class Tst<T extends I1, I2, I3>”和“class Tst2<T extends Cls & I1, I2, I3> {}”?

  23. 23

    为什么我可以在Python中实例化我的Abstract Base Class?

  24. 24

    为什么使用非空构造函数的Trait扩展Abstract Class进行编译?

  25. 25

    DBIx :: Class / SQL :: Abstract将SQL函数应用于WHERE子句中的列

  26. 26

    为什么我可以在Python中实例化我的Abstract Base Class?

  27. 27

    .class> .class和.class .class之间的区别

  28. 28

    “ .class”和“ .class,.class .class”之间的区别?

  29. 29

    在Java中将对象扩展抽象类的方法添加到类型为Abstract Class的Arraylist中的方法

热门标签

归档