Can an abstract class contain a public method?
abstract-class, c#
Solution
The justification for constructors on `abstract` types being `protected` is that there is simply no other entity that could call the constructor other than a derived type. Making the constructor `public` is meaningless in this case as it can't ever be invoked outside the type hierarchy. Hence the recommendation is to use `protected` as it's the most appropriate access modifier.
The same logic doesn't hold true with other members on the type. They can be freely invoked from outside the type hierarchy should their access modifier permit it.
public abstract class Dog {
// Public is appropriate here as any consumer of Dog could access
// Breed on an instantiated object
public abstract string Breed { get; }
// Public would be meaningless here. It's not legal to say
// 'new Dog' because 'Dog' is abstract. You can only say
// 'new Poodle' or 'new Bulldog'. Only derived types like
// Poodle or Bulldog could invoke the Dog constructor hence it's
// protected
protected Dog() { }
}
public class Poodle : Dog { }
public class Bulldog : Dog { }
Whether or not a particular member should be `public` or `protected` is highly dependent upon the particular API. The reasoning should be the exact same for `abstract` types as it is for non-abstract types
Problem
Constructors of an `abstract` `class` shouldn't be `public` and they should be `protected`. My question is about methods in that `abstract` `class`. Can we declare them as `public` or they should be `protected` too for the same reason?