English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define another class based on a class, which makes it easier to create and maintain applications. It also facilitates code reuse and saves development time.
When creating a class, the programmer does not need to rewrite the new data members and member functions completely. He just needs to design a new class that inherits the members of the existing class. This existing class is calledBase class,this new class is calledDerived class.
The concept of inheritance has realized Belongs to (IS-A) Relationship. For example, Mammal Belongs to (IS-A) Animal, dog Belongs to (IS-A) Mammal, therefore dog Belongs to (IS-A) Animal.
A class can derive from multiple classes or interfaces, which means it can inherit data and functions from multiple base classes or interfaces.
The syntax for creating a derived class in C# is as follows:
<AccessModifier> class <BaseClass>
{
...
}
class <DerivedClass> : <BaseClass>
{
...
}
Suppose, there is a base class Shape, whose derived class is Rectangle:
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 will produce the following result:
Total area: 35
Derived classes inherit the member variables and member methods of the base class. Therefore, the base class object should be created before the derived class object. You can initialize the base class in the member initialization list.
The following program demonstrates this:
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 will produce the following result:
Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5
Multiple inheritance refers to the ability of a class to inherit behaviors and features from more than one parent class. In contrast to single inheritance, single inheritance means that a class can only inherit from one parent class.
C# does not support multiple inheritanceHowever, you can use interfaces to implement multiple inheritance. The following program demonstrates this:
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 will produce the following result:
Total area: 35 Total paint cost: $2450