YouTip LogoYouTip

Csharp Inheritance

# C# Inheritance Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define one class based on another class, which makes creating and maintaining applications easier. It also facilitates code reuse and saves development time. When creating a class, a programmer does not need to completely rewrite new data members and member functions. They only need to design a new class that inherits the members of an existing class. This existing class is called the **base class**, and this new class is called the **derived class**. The concept of inheritance implements an **IS-A** relationship. For example, a mammal **IS-A** animal, a dog **IS-A** mammal, therefore a dog **IS-A** animal. ## Base Class and Derived Class A class can inherit from another class, known as the base class (parent class) and derived class (child class). C# does not support multiple inheritance of classes, but it supports multiple inheritance of interfaces. A class can implement multiple interfaces. **In summary:** A class can inherit from multiple interfaces, but only from one class. The syntax for creating a derived class in C# is as follows: class { ... } class: { ... } The derived class inherits the members (fields, methods, properties, etc.) of the base class, unless they are explicitly marked as private. The derived class can call the base class's constructors and methods using the keyword `base`. ## Example class BaseClass { public void SomeMethod() { // Method implementation } } class DerivedClass : BaseClass { public void AnotherMethod() { // Accessing base class method base.SomeMethod(); // Method implementation } } Assume there is a base class `Shape`, and its derived class is `Rectangle`: ## Example using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Derived class class Rectangle: Shape { public int getArea() { return(width * height); } } class RectangleTester { static void Main(string[] args) { Rectangle Rect =new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object Console.WriteLine("Total area: {0}", Rect.getArea()); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Total area: 35 ## Base Class Initialization The derived class inherits the member variables and member methods of the base class. Therefore, the parent class object should be created before the child class object. You can initialize the parent class in the member initialization list. The following program demonstrates this: ## Example using System; namespace RectangleApplication { class Rectangle { // Member variables protected double length; protected double width; public Rectangle(double l, double w) { length = l; width = w; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } }//end class Rectangle class Tabletop : Rectangle { private double cost; public Tabletop(double l, double w):base(l, w) {} public double GetCost() { double cost; cost = GetArea()*70; return cost; } public void Display() { base.Display(); Console.WriteLine("Cost: {0}", GetCost()); } } class ExecuteRectangle { static void Main(string[] args) { Tabletop t =new Tabletop(4.5, 7.5); t.Display(); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Length: 4.5Width: 7.5Area: 33.75Cost: 2362.5 ## Interface Inheritance An interface can inherit from one or more other interfaces. The derived interface inherits all members of the base interface. The derived interface can extend the member list of the base interface but cannot change their access modifiers. ## Example interface IBaseInterface { void Method1(); } interface IDerivedInterface : IBaseInterface { void Method2(); } An instance of interface inheritance can be implemented as follows: ## Example using System; // Define a base interface interface IBaseInterface { void Method1(); } // Define a derived interface, inheriting from the base interface interface IDerivedInterface : IBaseInterface { void Method2(); } // Class implementing the derived interface class MyClass : IDerivedInterface { public void Method1() { Console.WriteLine("Method1 implementation"); } public void Method2() { Console.WriteLine("Method2 implementation"); } } class Program { static void Main(string[] args) { // Create an instance of MyClass MyClass obj =new MyClass(); // Call the method inherited from the base interface obj.Method1(); // Call the new method added by the derived interface obj.Method2(); } } In the above example, the `MyClass` class implements the `IDerivedInterface` interface, so it must provide all methods defined in `IDerivedInterface`, including the `Method1()` method inherited from `IBaseInterface`. In the `Main` method, we create an instance `obj` of `MyClass` and call its methods. The output is: Method1 implementation Method2 implementation ## C# Multiple Inheritance Multiple inheritance refers to the ability of a class to inherit behavior and characteristics from more than one parent class. In contrast, single inheritance means a class can inherit from only one parent class. **C# does not support multiple inheritance**. However, you can use interfaces to achieve multiple inheritance. The following program demonstrates this: ## Example using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Base interface PaintCost public interface PaintCost { int getCost(int area); } // Derived class class Rectangle : Shape, PaintCost { public int getArea() { return(width * height); } public int getCost(int area) { return area *70; } } class RectangleTester { static void Main(string[] args) { Rectangle Rect =new Rectangle(); int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object Console.WriteLine("Total area: {0}", Rect.getArea()); Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area)); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Total area: 35Total paint cost: $2450
← Csharp PolymorphismCsharp Class β†’