There is an abstract class Figure with abstract methods. It is inherited by the abstract class Figure 2D, which defines the methods of the parent. Virtual methods are also added to the 2D2 class.

Questions:
1) Is it normal that in the abstract class Figure2D there are methods with the body (parent methods that need to be implemented)
2) Is it normal that an abstract class has virtual methods (that is, a class is both polymorphic and abstract)

ps: Implemented in c #

  • four
    Well, yes, if the task really requires it, and not the use of the PLO for the sake of the PLO - AseN

2 answers 2

  1. Yes. Although I personally don’t recall seeing a hierarchy of inheritance with two abstract classes. Generally, the further you practice OOP, the more you prefer simple hierarchies and composition.
  2. Yes. The abstract class is different from the interface, which may contain some default behavior, which may be contained in virtual methods.

    1) Is it normal that in the abstract class Figure2D there are methods with the body (parent methods that need to be implemented)

    The main difference between an interface and an abstract class is that an abstract class can contain not only an operation declaration, but also an implementation.

    At the same time, an abstract class may contain not only virtual methods (declaration + default implementation), but also may contain non-virtual methods that define the basis of some algorithm that is stable for all subclasses, but with some variable step, which is determined by a specific heir.

    Behind this intricate description is one of the most common design patterns, called the Template Method , which is fundamental to creating user-friendly type hierarchies.

    It looks like this:

    abstract class Shape {} abstract class Shape2D : Shape { public void Draw() { DrawFirstPart(); DrawSecondPart(); FillShape(); } protected abstract DrawFirstPart(); protected abstract DrawSecondPart(); protected abstract FillShap(); } 

    2) Is it normal that an abstract class has virtual methods (that is, a class is both polymorphic and abstract)?

    The presence of specific methods in the abstract class does not make it "concrete" or "polymorphic." Any abstract class will contain an implementation, otherwise you would use the interface.

    • Nuuuu ... Nevertheless, the interface differs from the abstract class more than the presence of implemented methods. Semantically, an interface is a contract, and an abstract class is a means of code reuse. - VladD
    • @VladD "Semantic interface is a contract" - interesting, here is the method signature - this is understandable, but if one or several signatures are grouped into a named block, then call it a contract. why? It seems to me that the contract is exactly the interface + implementation. - Stack
    • @Stack: A contract in the sense that if an object implementing an interface came to a method, I can be sure that this object has methods with these signatures and that they can be safely called. - VladD
    • @VladD "if an object that implements the interface came to the method to me" - this is understandable, in runtime you have an interface + implementation in the object - this is a contract. and the interface without implementation is a group of methods with certain signatures. - Stack
    • @Stack: No, the implementation is not part of the contract interface. Usually, as an architect, you do not want to impose such stringent requirements: “I expect method X and its concrete implementation here”, you only want “I expect method X here”. - VladD