C# Inheritance
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define a class based on another class, making the creation and maintenance of applications easier. It also facilitates code reuse and saves development time.
When creating a class, programmers do 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 the new class is called the derived class.
The concept of inheritance implements the is-a relationship. For example, a mammal is-a animal, a dog is-a mammal, and therefore a dog is-a animal.
Base Class and Derived Class
A class can be derived from multiple classes or interfaces, meaning it can inherit data and functions from multiple base classes or interfaces.
The syntax for creating a derived class in C# is as follows:
<access modifier> class <base class>
{
...
}
class <derived class> : <base class>
{
...
}
Suppose 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
A 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.ReadKey();
}
}
}
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.5
Width: 7.5
Area: 33.75
Cost: 2362.5
C# Multiple Inheritance
Multiple inheritance refers to the ability of a class to simultaneously inherit behavior and characteristics from more than one parent class. In contrast to single inheritance, where a class can only inherit from one parent class, multiple inheritance allows for multiple parent classes.
C# does not support multiple inheritance. However, you can achieve multiple inheritance using interfaces. 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 class 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: 35
Total Paint Cost: $2450