Home | » | C# | » | Inheritance |
---|
Inheritance means using the Pre-defined Code. This is very Main Feature of OOP With the advantage of Inheritance we can use any code that is previously created With the help of inheritance we uses the code that is previously Defined but always Remember We are only using that code but not Changing that code With the Advent of inheritance we able to use pre-defined code and also able to add new code. All the pre-defined code is reside into the form of classes if we wants to use that code then we have to inherit or extend that class The Class that is Pre-defined is called as Base or super Class and the class which uses that code is called as derived or sub class There many types of Inheritance provided by C# But Multiple Inheritance is not Provided by C#. Various types of inheritance provided by C# is as follows:
Contents
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hiding through Inheritance
- Inheritance and Constructors
- Containment Class
- Method Overriding
Hiding through Inheritance
Name hiding through inheritance occurs when classes or structs redeclare names that were inherited from base classes. The type of name hiding takes one of the following forms.
- A constant, field, property, event, or type introduced in a class or struct hides all base class members with the same name.
- A method introduced in a class or struct hides all non-method base class members with the same name, and all base class methods with the same signature.
- An indexer introuced in a class or struct hides all base class indexers with the same signature (parameter count and types).
Output
Inheritance and Constructors
The constructors and destructors are not inherited to a derived class from a Base class. However when we crate an object of the derived class, the derived class constructor implicitly call the base default constructor. The following prgram show this.
Containment Class
With the Advent of C# we can Also Develop Also Some Another Form of Inheritance which is also known as the m Containment . Inheritance is Called as Containment when we are Declaring the Object of a class into another Class Then They have a Relationship Like house has a Room and Car has a Stereo etc.
Method Overriding
In inheritance a user may have to face Problem about using the Same name Method in both the Classes like in Base and Derived Classes there is Same Name of a Class so that if we wants to Define a Method which is Similar to base class in a derived Class then we have to use two Keywords as Virtual and Override.
The Method which is declared as Virtual in a base class , can only be Override by derived Class, if in the Derived Class uses Override Keyword along with the Name of Method which we wants to Defined Again. And if you wants to Access the Method of Base Class then we have to use the Base.