Hello Everybody I am lokesh kumar today we will be learn what is inheritance and it's type
so i will be start from inheritance Definition of Inheritance.
What is Inheritance
Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.
The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by using inheritance.
The following are the types of inheritance in C#.
The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.
Base class: is the class from which features are to be inherited into another class.
Derived class: it is the class in which the base class features are inherited.
Single inheritance
It is the type of inheritance in which there is one base class and one derived class.
Inheritance is a mechanism of acquiring the features and behaviors of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance implements the IS-A relationship.
For example, mammal IS-A animal, dog IS-A mammal; Hence dog IS-A animal as well.
Advantages
Reduce code redundancy.
Provides code reusability.
Reduces source code size and improves code readability.
Code is easy to manage and divided into parent and child classes.
Supports code extensibility by overriding the base class functionality within child classes.
Disadvantages
In Inheritance base class and child classes are tightly coupled. Hence If you change the code of parent class, it will get affects to the all the child classes.
In class hierarchy many data members remain unused and the memory allocated to them is not utilized. Hence affect performance of your program if you have not implemented inheritance correctly.
For example:
public class Accountcreditinfo //base class
{
public string Credit()
{
return "balance is credited";
}
}
public class debitinfo : Accountcreditinfo //derived class
{
public string debit()
{
Credit(); ////derived class method
return "balance is debited";
}
}
In the preceding sample program Accountcreditinfo is the base class and debitinfo is the derived class.
Hierarchical inheritance
This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.
This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.
For example:
class A //base class
{
public string msg()
{
return "this is A class Method";
}
}
class B : A
{
public string info()
{
msg();
return "this is B class Method";
}
class C : A
{
public string getinfo()
{
msg();
return "this is B class Method";
}
}
}
In the preceding program one base class is derived in many classes hence it is a called a Hierarchical Inheritance.
Multilevel inheritance
When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.
For example:
public class Person
{
public string persondet()
{
return "this is the person class";
}
}
public class Bird : Person
{
public string birddet()
{
persondet();
return "this is the birddet Class";
}
}
public class Animal : Bird
{
public string animaldet()
{
persondet();
birddet();
return "this is the Animal Class";
}
}
In the preceding program, each class is derived from one class that is derived from another class hence this type of inheritance is called Multilevel Inheritance.
Multiple inheritance using Interfaces
C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces, we will see more about interfaces in my next article in detail.
For example:
public interface IA //ineterface 1
{
string setImgs(string a);
}
public interface IB //Interface 2
{
int getAmount(int Amt);
}
public class ICar : IA, IB //implementatin
{
public int getAmount(int Amt)
{
return 100;
}
public string setImgs(string a)
{
return "this is the car";
}
}
In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.
The following are some key points about inheritance:
C# does not support multiple inheritances of classes, the same thing can be done using interfaces.
Private members are not accessed in a derived class when one class is derived from another.
Summary
I hope you now have an overview of inheritance and its types. In the next article we will learn about each inheritance type in detail. If you have any suggestions regarding this article then please contact me.
For example:
class A //base class
{
public string msg()
{
return "this is A class Method";
}
}
class B : A
{
public string info()
{
msg();
return "this is B class Method";
}
class C : A
{
public string getinfo()
{
msg();
return "this is B class Method";
}
}
}
In the preceding program one base class is derived in many classes hence it is a called a Hierarchical Inheritance.
Multilevel inheritance
When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.
For example:
public class Person
{
public string persondet()
{
return "this is the person class";
}
}
public class Bird : Person
{
public string birddet()
{
persondet();
return "this is the birddet Class";
}
}
public class Animal : Bird
{
public string animaldet()
{
persondet();
birddet();
return "this is the Animal Class";
}
}
In the preceding program, each class is derived from one class that is derived from another class hence this type of inheritance is called Multilevel Inheritance.
Multiple inheritance using Interfaces
C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces, we will see more about interfaces in my next article in detail.
For example:
public interface IA //ineterface 1
{
string setImgs(string a);
}
public interface IB //Interface 2
{
int getAmount(int Amt);
}
public class ICar : IA, IB //implementatin
{
public int getAmount(int Amt)
{
return 100;
}
public string setImgs(string a)
{
return "this is the car";
}
}
In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.
The following are some key points about inheritance:
C# does not support multiple inheritances of classes, the same thing can be done using interfaces.
Private members are not accessed in a derived class when one class is derived from another.
Different Types of Inheritance
OOPs supports the six types of inheritance as given below-
Single inheritance
In this inheritance, a derived class is created from a single base class.
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
Multi-level inheritance
In this inheritance, a derived class is created from another derived class.
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : B
{
public void fooC()
{
//TO DO:
}
}
Multiple inheritance
In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F# etc.
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Base Class
class B
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : A, B
{
public void fooC()
{
//TO DO:
}
}
Multipath inheritance
n this inheritance, a derived class is created from another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : A
{
public void fooC()
{
//TO DO:
}
}
//Derived Class
class D : B, A, C
{
public void fooD()
{
//TO DO:
}
}
Hierarchical inheritance
In this inheritance, more than one derived classes are created from a single base.
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : A
{
public void fooC()
{
//TO DO:
}
}
//Derived Class
class D : C
{
public void fooD()
{
//TO DO:
}
}
//Derived Class
class E : C
{
public void fooE()
{
//TO DO:
}
}
//Derived Class
class F : B
{
public void fooF()
{
//TO DO:
}
}
//Derived Class
class G :B
{
public void fooG()
{
//TO DO:
}
}
Hybrid inheritance
This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.
Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritance is not supported by .NET Languages.
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Base Class
class F
{
public void fooF()
{
//TO DO:
}
}
//Derived Class
class B : A, F
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : A
{
public void fooC()
{
//TO DO:
}
}
//Derived Class
class D : C
{
public void fooD()
{
//TO DO:
}
}
//Derived Class
class E : C
{
public void fooE()
{
//TO DO:
}
}
nheritance in the real world objects
For instance, take the example of a store that sells computer related hardware and software. Both hardware and software are products. Think of these real world objects in terms of classes. Both software and hardware products have a name, price and company which made them. These fields are common. Similarly, the class’ “Purchase” method can be common between the two classes. But, there are some properties intrinsic to software and hardware. For instance, software can have a version property; similarly it can have an Install method. Hardware can have a model property and it can have a Ship method.
The idea of inheritance is to group member variables and methods that exist in multiple classes into one parent class and then inherit that parent class in the classes which originally contained common variables and methods. All those real world objects which have ‘is a’ relationship between them can be implemented via inheritance. In the aforementioned example, both software and hardware have is a relation with a product. Software ‘is a’ product similarly hardware ‘is a’ product.
Learn more about C# Inheritance at Udemy.com
Understanding C# Inheritance
In C#, inheritance is simple to implement; the parent or the base class contains common methods and variables whereas the child or the derived classes contain unique variable and methods. A ‘:’ is used to derive a base class. In C# inheritance, a class can inherit from only one class but one class can be inherited by multiple classes. In the following example, the aforementioned inheritance relation between parent class Product and its child classes Hardware and Software, has been demonstrated.
This is how a parent class Product would look like:
Product Class
class Product
{
public string name;
public string type;
public int price;
public Product(string name, string type, int price)
{
this.name = name;
this.type = type;
this.price = price;
}
public void Purchase()
{
Console.WriteLine("You have purchased the product " + this.name);
}
}
The class has member variables name, type and price; a constructor which is used to initialize the fields and a method Purchase which simply displays the name of the product purchased by the user.
Any class which inherits the Product class will not have to write these member variables and Purchase method; rather they will be automatically available to child classes. This is how inheritance fosters reusability. The definitions of the Software and Hardware classes that inherit this parent class have been demonstrated below:
Software Class
class Software : Product
{
public double version;
public int size;
public Software(double version, int size, string name, string type, int price):base(name, type, price)
{
this.version = version;
this.size = size;
}
public void Install()
{
Console.WriteLine("Your software is being installed ...");
}
}
Hardware Class
class Hardware : Product
{
public double model;
public double weight;
public Hardware(double model, double weight, string name, string type, int price):base(name, type, price)
{
this.model = model;
this.weight = weight;
}
public void Ship()
{
Console.WriteLine("Your product will be shipped in 10 days");
}
}
You can see that both Software and Hardware class have different member variables and methods. Software class has an Install method whereas Hardware class has Ship method. The member variables of the two classes are also different. But since both of them are inheriting the Product class, they share some common methods and variables.
Another interesting thing to note here is the constructor of both Software and Hardware classes. They take 5 parameters though they initialize only two member variables. The remaining three parameters are passed to the constructor of the base class using colon operator “:” followed by “base” keyword and the parameter names. This is another effective way of calling base class constructor in C# Inheritance. In the Main method, following code can written to see how inheritance relationship actually works:
Software schild = new Software(3.2, 4000, "Game X", "Software", 100);
Hardware hchild = new Hardware(4.8, 2.5, "Laptop", "Hardware", 500);
schild.Purchase();
schild.Install();
hchild.Purchase();
hchild.Ship();
Notice that Software class did not contain any definition for Purchase method, yet it can call it because it is inheriting Product class which contains definition for Purchase method.
Seems difficult? Learn the basics of C# at Udemy.com
Virtual Methods
In C# inheritance, Virtual methods in the parent class can be overridden by the child classes. Virtual methods are of particular advantage where derived classes want to change the definition of a method described in the parent class. If child classed do not implement their own definition of the method, then the default definition in the parent method can be used. To make a method virtual, keyword “Virtual” is used. To see how virtual methods actually affect inheritance, make following changes in the code already discussed;
Change Purchase method of the Product class as follows:
public virtual void Purchase()
{
Console.WriteLine("You have purchased the product " + this.name);
}
Notice, Purchase method has been modified with keyword virtual. Similarly, insert following method into the Software class:
public override void Purchase()
{
Console.WriteLine("You have purchased a software named :"+base.name);
}
This method will override the parent class purchase method. No changes should be made in the Hardware class. Now if Purchase method from the object of Software class is called, it will call its own overridden Purchase method, whereas if Purchase method is called from the object of Hardware class, the Purchase method of the parent class Product will be called since, Hardware class doesn’t override the virtual Purchase method of the Product class.
In C#, inheritance is a vast domain; it includes abstract classes, abstract methods, interfaces, casting, sealing and many more advanced concepts.





