- Inheritance is one of the primary concepts of object oriented programming.
- Inheritance is the process with which an object of one class acquires properties or methods from an object of another class.
- The concept behind inheritance, take the base class that contains methods and properties and inherits it into the derived class, that now contains all the members and properties of the base class as well as its own newly added members or properties.
Syntax: <Interface_ Modifer> interface <Interface_ identifier > : <base_interface>
- The following are the advantages of Inheritance:
- Reuse the existence code
- Save time, we need not do the code verification and testing.
- Here, we can add and modify the methods of the existing class.
- Help in modularization of code.
- Private members of the base class will not be inherited to the derived class
- Structures (struct) in C# does not support inheritance, it can only implements interfaces.
Types of Inheritance:
The following are the types of inheritance...- Single level inheritance: Here we have single base class that is inherited by the derived class. Here the derived class has all the features of the base class and can add new features or modify existing features. The inheritance also depends on the access specifier that is used at the time of base class inheritance.
- Multi level inheritance: In the multi level inheritance, here we having a chain of inheritance i.e. the base class A is inherited by derived class B, and it is further inherited by another derived class C. So, the feature we have in base class A, is also there in derived class B, and all the combination features of class A and class B are there in derived class C.
- Hierarchical inheritance: It is the process of deriving two or more classes from single base class. And in turn each of the derived classes can further be inherited in the same way. Thus it forms hierarchy of classes or a tree of classes which is rooted at single class.
- Multiple Inheritance: Multiple inheritance is a concept of one class can inherit two base classes. So, the multiple inheritance can be done by using the concept of interfaces. Here one class can inherit one class and can implements more than one interface.
- Hybrid Inheritance: The hybrid inheritance can also be done through with the help of interface. Here the derived class can implements more than two interfaces and only one class.
Note: C# doesn't support multiple inheritance and hybrid inheritance. We can achieve these with the support of Interfaces. I'll add the usage of interfaces in the next update.
QUESTION: Does C# support multiple inheritances?
ANSWER: No, use interfaces instead. In C# a class can inherit from only one base class, however a class can implements many interface, which servers some of the same purpose without increasing complexity. So that you can implements more than one interface, thus multiple inheritances is achieved through interface.
0 comments :
Post a Comment