Inheritance
Inheritance is of the fundamental attributes of Object-Oriented Programming(OOPs) because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of C#, a class that is inherited is called a superclass (The class whose members are inherited is called a base class). “The class that is not inheriting is called a subclass”. The word inheritance defines a property by virtue of which a class acquires some features from another class. It allows the objects of one class can to link and share some common features from the objects of another class.
There are two categories in the inheritance concept:-
- Derived Class – The class that inherits from other class (Child).
- Base Class – The class being inherited from (Parent).
Hence, Inheritance is a technique by virtue of which a class acquires the properties and features (i.e. Data members and member methods) from another class.
Let us take a real-world example:
With reference in the above illustration, a base class ‘Car’ has three derived classes via. FORD Class, MUSTANG Class, ( referred to as hierarchical Inheritance).
Example
class Vehicle // base class (parent)
{
public string brand = "Ford";
public void honk()
{
Console.WriteLine("Tuut, tuut!");
}
}
class Car: Vehicle // derived class (child)
{
public string modelName = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.honk();
Console.WriteLine(myCar.brand + " " + myCar.modelName);
}
}
Need of Inheritance
The base class simply defines the common functional to be used by other classes. Once, a base class is compiled and machine code is generated, you don’t need to recompile it again at the time of accessing it into derived classes. The machine code of base class used by a derived class can also be reused by another derived class for common operations. This feature is termed as ‘Reusability’ and it is made possible only when a class inherits from another class.
Reusability feature of inheritance provides the following benefits:
- It allows the user to model real world relationship.
- If any change is made in the base class, it is carried to all its derived classes. Let X, Y and Z are three classes such that classes Y and Z inherits from class X then any change brought in class X will reflect on the execution of the classes Y and Z
- Inheritance shows transit nature. It means, if son derives from father and the derives from grandfather then son will implicity gain the capability of using the features from grandfather.
There are three types of inheritance:-
Single Heritance
In single Inheritance, one class inherits the properties from another class. It enables a derived class to inherit the characteristics and behaviour from single base class. Thus, it enables the code reusability as well as add new features to the existing code.
How Execute Single Inheritance:
Class X
{
public String name,
private int roll;
protected int marks;
———————
———————
}
Class Y extends X
{
Public double per;
Private char grade;
Protected String result
———————–
———————–
}
The example shown above has two classes X and Y such that a single base class X gets inherited into a single derived class Y. Hence, it is single inheritance.
using System;
namespace studytonight
{
public class Parent
{
public void displayParentsAB()
{
Console.WriteLine("A and B are my parents");
}
}
public class Son: Parent
{
public void DisplaySonC()
{
Console.WriteLine("I am the son C");
}
}
public class program
{
public static void Main(String[] args)
{
Son s = new Son();
s.DisplaySonC();
s.DisplayParentsAB();
}
}
}
Multilevel Inheritance
This another type of inheritance in which a sub-class derived from a base acts as a base for another target. It means a program may have more than one ‘Base’ classes but at different levels. This type of inheritance is referred to as ‘Multilevel’ Inheritance. Here, there are two successive ‘Base’ classes. The class A is the base for class B and class B in turn will be the base for class C. Thus, class C implicity inherits the characteristics and behaviour of class A along with class B. Multilevel inheritance is also referred to as ‘Nested Inheritance’
How to Execute Multilevel Inheritance:
class X
{
Public String name;
Private int roll;
Protected int marks;
———————–
———————–
———————–
}
class Y extends X
{
public doule per;
private char grade;
protected String result
————————
————————
————————
}
class Z extends Y
{
public int tutfee;
private int misc;
protected in total;
———————
———————
———————
}
The example shown above uses three classes X,Y and Z such that base class X is inherited in target Y. Class Y acts as base that is further inherited into class Z. Hence, the above mentioned structure is nested or multilevel type of inheritance.
Example:
using System;
namespace Studytonight
{
public class Grandparent()
{
Concole.WriteLine("Constructor called at run-time");
}
public void DisplayGrandParentsAB()
{
Console.writeLine("A and B are my grandparents");
}
}
public class Parents : GrandParent
{
public void DisplayParentsCD()
{
Console.WriteLine("C and D are my parents");
}
}
public class Child: Parent
{
public void DisplayChildZ()
{
Console.WriteLine("I am the child Z");
}
}
public class program
{
public static void Main(String[] args)
{
Child cd = new Child();
cd.DisplayChildZ();
cd.DisplayParentsCD();
cd.DisplayGrandParents();
}
}
}
Multiple Inheritance
when a sub class inherits from multiple base classes, it is known as multiple inheritance. C# does not support multiple type of inheritance i.e. a number of classes cannot be extended into a single base. To sort out this problem, C# language uses interface. A number of interfaces can be implemented in a single Sub-class.
Example:
class FruitColors {
public void colors()
{
List MyList = new List();
MyList.Add("Red");
MyList.Add("Yellow");
MyList.Add("Green");
MyList.Add("Orange");
Console.WriteLine("Fruits colors List : ");
foreach(var elements in MyList)
{
Console.WriteLine(elements);
}
}
}
class FruitsDetails : Fruits, Fruitcolors {
}
public class GetDetails {
static public void Main()
{
FruitsDetails obj = new FruitsDetails();
obj.MyFruits();
obj.colors();
}
}
Hierarchical Inheritance
In this case, a ‘Base’ (super class) has more than one ‘Derived’ classes (sub – classes) or you can say, more than one derived classes have the same Base class. This type of extending characteristics and behaviour is referred to as hierarchical inheritance.
How to Execute Hierarchical Inheritance:
Class X
{
Public String name;
Private int marks;
——————–
——————–
——————–
}
Class Y extends X
{
public double per;
private char grade;
protected String result;
————————–
————————–
————————–
}
class Z extends X
{
public int tutfee;
private int misc;
protected int total;
———————-
———————-
———————-
}
In the example shown above, the sub classes Y and Z inherit from the base class X.
Example:
using System;
namespace StudyTonight
{
public class Parent
{
public void DisplayParentsAB()
{
Console.WriteLine("A and b are my parents");
}
}
public class ChildC : Parent
{
public void DisplayChildC()
{
Console.WriteLine("I am the child c");
}
}
public class ChildD : Parent
{
public void DisplayChild()
{
Console.WriteLine("I am the child D");
}
}
public class program
{
public static void MAin(String[] args)
{
ChildC cc = new ChildC();
ChildD dd = new ChildD();
cc.DisplayChildC();
cc.DisplayParentsAB(); //accessing parent class
cd.DisplayChildD();
cd.DisplayParentsAB(); //accessing parent class
}
}
}
This illustrates hierarchical type of inheritance. In this system, a single base is derived by multiple targets.
Hybrid Inheritance
When two or more types of inheritance systems are combined, it is called as ‘Hybrid’ inheritance. In the illustration shown along side, you can notice that all the types of inheritances are used together. In the illustration shown alongside, class B and class C both inherit from class A became a hierarchical inheritance. Further, class B inherits from class A and in turn acts as a base for class D, is multilevel inheritance. Class D derives from class class B and C. Hence, it is multiple inheritance.
When more than one type of inheritance systems are used together, it is known as hybrid inheritance.
Example:
interface A
{
// code here
}
interface B : A
{
// code here
}
interface C : A
{
// code here
}
// Derived class
clas D : B, C
{
//code here
}
Hybrid inheritance is a combination of different types of inheritance systems all together. We are not showing any example due to the reason that you may need multiple inheritance at the point of illustration. This cannot be resolved by using extend keyword. All you need is the application of interfaces to sort out the problem.