Can I set rules for the class inheriting my base class. eg. Person : BaseClass
, I want Person
to implement iSomeKindOfInterface
, if Person
does not implement the interface, it is not allowed to inherit from BaseClass
.
I know this is posibble in generic base classes where you can do the following
public BaseClass<T>
where T : iSomeKinfOfInterface
Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
In this, various Child classes inherit a single Parent class. The example given in the introduction of the inheritance is an example of Hierarchical inheritance since classes BMW and Audi inherit class Car. Here two child classes are inheriting the same Parent class.
You can implement the interface in your base class and force the inheriting class to supply the implementation:
public interface ISomeInterface
{
void DoSomething();
}
public abstract class BaseClass : ISomeInterface
{
public abstract void DoSomething();
}
public class Person : BaseClass
{
public override void DoSomething()
{
...
}
}
Declare your class as
abstract BaseClass : ISomeKinfOfInterface
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With