Abstract classes and methods in Java, Inheritance

后端 未结 5 888
星月不相逢
星月不相逢 2021-02-14 15:22

I have class B, which inherits from class A. The superclass A is abstract, containing one abstract method. I don\'t want to implement the abstract method in class B, therefore I

5条回答
  •  走了就别回头了
    2021-02-14 15:56

    In Java, the abstract class annotation indicates that the class cannot be directly instantiated. A class could be declared abstract simply because it should never be instantiated (perhaps it contains only static methods), or because its subclasses should be instantiated instead.

    It is not a requirement that abstract classes contain abstract methods (the inverse is true: a class containing one or more abstract methods must be abstract.)

    The question of whether you should duplicate the abstract method definition might be perceived as a style question - but I would be hard pressed to come up with an argument in favor of duplicating the definition (the only argument I can come up with is in the case where the class hierarchy might change the semantics or use of the method, and thus you'd like to provide an additional javadoc in class B.)

    The primary argument against re-definition of the abstract method is that duplicate code is bad - it makes refactoring more cumbersome and such (all the classic "don't duplicate code" arguments apply.)

提交回复
热议问题