How is Inheritance achieved in C#?

Inheritance is where you have a generalized class whose behavior is extended in specialized class.

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:

class sampleShape {
int arg1, arg2;
public virtual int area() {
Console.WriteLine(“Virtual method area of base class”);
}
}
class sampleRectangle:sampleShape {
public sampleRectangle(int arg1, int arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
public override int area() {
return (arg1 * arg2);
}
}
class sampleTriangle:sampleShape {
public sampleTriangle(int arg1, int arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
public override int area() {
return ((arg1/2) * arg2);
}
}
class testClass {
sampleShape obj = new sampleRectangle(10,20);
int result1 = obj.area();
obj = new sampleTriangle(10,20);
int result2 = obj.area();
Console.WriteLine(“Area of Rectangle:{0}, Area of Triangle:{1}”, result1, result2);
}

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:

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:

class baseClass {
public virtual void displayMsg() {
System.Console.WriteLine("Executing baseClass method...");
}
}
class derivedClass1 : baseClass {
public void displayMsg1() {
System.Console.WriteLine("Executing derivedClass1 method… ");
}
}
class derivedClass2 : derivedClass1 {
public void displayMsg2() {
System.Console.WriteLine("Executing derivedClass2 method…");
}
}

class testClass {
public static void Main() {
derivedClass2 obj = new derivedClass2();
obj.displayMsg();
obj.displayMsg1();
obj.displayMsg2();
}
}

Output of this code will be:

Executing baseClass method…
Executing derivedClass1 method…
Executing derivedClass2 method…

Note that derived class can access and override methods of all its parents in the hierarchy and not just its immediate base class.

| 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#? |

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.