How is Inheritance achieved in C#?
is where you have a generalized class whose behavior is extended in specialized
Great Savings... Everyday!
Tuesday, January 19th, 2021
CHECK NOW >>
For example, there are different shapes namely square, rectangle, octagon and many others. All of them have common characteristics of a shape, for example Area. You can compute area of any shape but each shape has its own formula for computing area. Such common characteristics will be captured in the class shape which will then be inherited by specialized classes like square and rectangle.
There are different types of inheritance. They are pictorially represented with real time examples in the diagram shown above. Apart from the simple inheritance, hierarchical inheritance, multiple inheritance and multilevel inheritance, you can also have a hybrid situation wherein you have more than one type of inheritance jumbled together.
In C#, you achieve multiple inheritance using interface. All other types of inheritance can be achieved by extending base class in derived class. Interface is a separate feature on its own. Hence this article will cover simple inheritance, hierarchical inheritance and multilevel inheritance. Given below are examples of each type of inheritance implemented using C#:
Example for Simple Inheritance and Hierarchical Inheritance:
Output of this code will be:
Area of Rectangle:200, Area of Triangle:100
Here sampleRectangle derives from sampleShape, this demonstrates simple inheritance. However, sampleTriangle also inherits from sampleShape and both the derived classes override area method to implement the corresponding formula. This demonstrates hierarchical inheritance.
Multilevel inheritance is where base class is derived by the derived class. And the derived class is inherited further by next level of derived class. And this leveling might continue for any level based on your requirement. Here is an example to demonstrate multilevel inheritance:
Output of this code will be:
derived class can access and override methods of all its parents in the
hierarchy and not just its immediate base class.
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
| How do you implement Observer Design Pattern in .NET? | How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |