- C# is a single-inheritance programming language.
- This means that a class cannot derive from two classes directly.
- When we need multiple inheritance class structures, we can use aggregation.
- We can implement the second class by making a field an instance of the second class.
Overriding the Base Class
public
and protected
members of a base class are inherited in the derived class.
protected
Members
- These members can only accessed from derived classes instances and not from base class instances.
- They are accessible inside the class implementation.
- Declaring a protected member in a sealed class is the same as declaring it as private.
virtual
Modifier & override
Keyword
- You can override instance methods and properties but not fields or any static members.
- The base class must mark each member for which it allows overriding as
virtual
.
- If
public
or protected
members do not include the virtual modifier, then sub-classes will not be able to override those members.
- It is required that the overriding methods to use the
override
keyword explicitly.
- Whenever the runtime encounters a
virtual
method, it calls the most derived and overriding implementation of the virtual member.
- Converting a method from a virtual method to a non-virtual method could break derived classes that override the method.
sealed
Classes
sealed
classes cannot be derived.
sealed
classes cannot be abstract.
- The
string
class is an example of a sealed class.
sealed
members prevent an override of a virtual member to be overridden be another subclass.
- This negates the virtual aspect of the member for any further derived class.
sealed
Modifier