Abstract class and constructor

How can an abstract object not be created, why is the constructor still allowed inside the abstract class?

public abstract class SomeClass 
 {  
     private string _label;

     public SomeClass(string label)  
     {  
         _label=label;
     }  
}
+3
source share
2 answers

Constructors of any derived class will still have to call the constructor in the abstract class. If you do not specify any constructors at all, all derived classes just have to use the parameterless parameter provided by the compiler.

It makes perfect sense to have a constructor, but in this case, "public" is really equivalent to "protected".

+11
source

Since you can still do the following:

public class SomeChildClass : SomeClass
{
    public SomeChildClass(string label) : base(label){ }

    public string GetLabel() { return _label; }
}

, ( ), .

, public . , protected.

+3

Source: https://habr.com/ru/post/1740781/


All Articles