Inheritance is a form of software reuse in which a new class is created by absorbing an existing class’s members and enhancing them with new or modified capabilities. With inheritance, you save time during app development by reusing proven and debugged high-quality software.
A derived class is more specific than its base class and represents a more specialized group of objects.
The is-a relationship represents inheritance. In an is-a relationship, an object of a derived class also can be treated as an object of its base class.
Inheritance relationships form treelike hierarchical structures. A base class exists in a hierarchical relationship with its derived classes.
Objects of all classes that extend a common base class can be treated as objects of that base class. However, base-class objects cannot be treated as objects of their derived classes.
When a base-class method is inherited by a derived class, that derived class often needs a customized version of the method. In such cases, the derived class can override the base-class method with an appropriate implementation.
protected
MembersUsing protected
access offers an intermediate level of access between public
and private
. A base class’s protected
members can be accessed by members of that base class and by members of its derived classes.
Base-class members retain their original access modifier when they become members of the derived class.
Methods of a derived class cannot directly access private
members of the base class.
CommissionEmployee
ClassA colon (:) followed by a base-class name at the end of a class declaration header indicates that the declared class extends the base class.
If a class does not specify that it inherits from another class, the class implicitly inherits from object
.
The first task of any derived class’s constructor is to call its direct base class’s constructor, either explicitly or implicitly (if no constructor call is specified).
Constructors are not inherited. Even if a class does not have constructors, the default constructor that the compiler implicitly declares for the class will call the base class’s default or parameterless constructor.
Method ToString
is one of the methods that every class inherits directly or indirectly from class object
, which is the root of the C# class hierarchy.
To override a base-class method, a derived class must declare a method with keyword override
and with the same signature (method name, number of parameters and parameter types) and return type as the base-class method. Also, the base-class method must be declared virtual
.
It’s a compilation error to override a method with a different access modifier.
BasePlusCommissionEmployee
Class without Using InheritanceCopying and pasting code from one class to another can spread errors across multiple source-code files. To avoid duplicating code (and possibly errors) in situations where you want one class to “absorb” the members of another class, use inheritance.
CommissionEmployee–BasePlusCommissionEmployee
Inheritance HierarchyThe virtual
and abstract
keywords indicate that a base-class property or method can be overridden in derived classes.
The override
modifier declares that a derived-class method overrides a virtual
or abstract
base-class method. This modifier also implicitly declares the derived-class method virtual
.
When a base class’s members are private
, a derived class’s members are not allowed to access them.
CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy Using protected
Instance VariablesInheriting protected
instance variables enables you to directly access the variables in the derived class without invoking the set
or get
accessors of the corresponding property.
Software is said to be fragile or brittle when a small change in the base class can “break” derived-class implementation. You should be able to change the base-class implementation while still providing the same services to the derived classes.
Declaring base-class instance variables private
enables the base-class implementation of these instance variables to change without affecting derived-class implementations.
CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy Using private
Instance VariablesPlace the keyword base
and the member access (.
) operator before the base-class method name to invoke an overridden base-class method from a derived class.
Failure to prefix the base-class method name with the keyword base
and the member access (.
) operator when referencing the base class’s method causes the derived-class method to call itself, creating infinite recursion.
Instantiating a derived-class object begins a chain of constructor calls. The last constructor called in the chain is always the constructor for class object
. The original derived class constructor’s body finishes executing last.
We can customize new classes to meet our needs by including additional members and by overriding base-class members.
object
All classes in C# inherit directly or indirectly from the object
class, so its seven methods are inherited by all other classes. These methods are Equals
, Finalize
, GetHashCode
, GetType
, Member-wiseClone
, ReferenceEquals
and ToString
.