Creating a new class from existing class is called as inheritance.
When a new class needs same members as an existing class, then instead of creating those members again in new class, the new class can be created from existing class, which is called as inheritance.
During inheritance, the class that is inherited is called as base class and the class that does the inheritance is called as derived class and every non private member in base class will become the member of derived class.
In C# we use a colon(:) symbol to inherit a class.
Different Types of Inheritance
C# supports 4 types of Inheritance which are following :
Single Inheritance:
In this inheritance, a derived class is created from a single base class.
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.
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.
Advantages
1. Reduce code redundancy.
2. Provides code reusability.
3. Reduces source code size and improves code readability.
4. Code is easy to manage and divided into parent and child classes.
5. Supports code extensibility by overriding the base class functionality within child classes.
5. Supports code extensibility by overriding the base class functionality within child classes.
Disadvantages
1. 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.
2. 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.
0 comments:
Post a Comment