C# Tutorial - Reasons for InheritanceInheritance can be implemented in a variety of ways for a variety of reasons. Listed below are some of those reasons. Inheritance for SpecializationThe most common use of inheritance is to subclass for specialization, this way a special version of the parent class is created that still satisfies the specification for the parent class. The child class subtype is substitutable for the parent class and so the principles of substitutability are upheld. Inheritance for SpecificationA frequent use of inheritance is to ensure that classes maintain a common interface. Each of the child classes has the same interface that implements the methods declared in the parent class. Often the parent class is an abstract class that cannot be created as an object itself. Inheritance for ExtensionThe child class keeps the functionality of the parent class intact without changing or overwriting any of it. It then adds its own functionality to extend that of the parent class, changing the interface overall. Inheritance for ConstructionThis use of inheritance is where the child class requires all of the desired functionality offered by the parent class, but there is no abstract relationship between the child and the parent class. The child class is not a form of the parent class. Inheritance for GeneralizationA form of inheritance generalizes the functionality found in the parent class by overriding the methods in the child class. Inheritance for LimitationSometimes there is a need to limit the interface of the parent class within the child class. This is not a very desirable method of inheritance, but can be managed by limiting the parent class in the first place and creating a second child class with the additional functionality that was in the original parent class. Inheritance for CombinationThis is similar to construction, but the child class requires elements from two classes. Multiple-inheritance is not possible through C#, however a child class can inherit from a parent class whilst implementing an interface also. InterfacesAn interface looks like a class, but has no implementation. They can contain methods, properties, events, and indexers (a member that enables an object to be indexed similar to an array). Following from the previous inheritance example, we can create an interface: interface IIntelligence
{
// Method
bool behaviour();
// Property
int IQ
{
get;
set;
}
}
Interfaces are implemented in the same manner as inheritance through the colon (:). When implementing interfaces, a comma (,) is used to separate the interfaces/or classes that are being derived from. So the Terrier class can implement the IIntelligence interface. public class Terrier : Dog, IIntelligence
{
public Terrier()
: base()
{
}
private string species = "Terrier";
public override string getSpecies() { return species; }
private int _iq;
public int IQ
{
get { return _iq; }
set { _iq = value; }
}
public bool behaviour()
{
return true;
}
}
The Terrier class now inherits from the Dog class and implements the IIntelligence interface. Thus Inheritance for Combination is demonstrated.
|