C# Inheritance
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define another class based on one 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 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 thebase class, this new class is calledderived class。
The concept of inheritance implementsIs-a (IS-A)relationship. For example, mammalsIs-a (IS-A)Animal, DogIs-a (IS-A)mammals, therefore dogsIs-a (IS-A)Animal.
Base classes and derived classes
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 can only inherit from one class.
The syntax for creating a derived class in C# is as follows:
{
...
}
class <derived class> : <base class>
{
...
}
A derived class inherits the members of the base class (fields, methods, properties, etc.), unless they are explicitly marked as private.
A derived class can call the base class's constructors and methods through the keyword `base`.
Example
{
public void SomeMethod()
{
// Method implementation
}
}
class DerivedClass : BaseClass
{
public void AnotherMethod()
{
// Accessing base class method
base.SomeMethod();
// Method implementation
}
}
Suppose there is a base class Shape, and its derived class is Rectangle:
Example
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:
总面积: 35
Initialization of the base class
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 is created. You can perform the parent class initialization in the member initializer list.
The following program demonstrates this:
Example
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:
长度: 4.5 宽度: 7.5 面积: 33.75 成本: 2362.5
Interface Inheritance
An interface can inherit from one or more other interfaces, and the derived interface inherits all members of the base interface.
A derived interface can extend the member list of the base interface, but cannot change their access modifiers.
Example
{
void Method1();
}
interface IDerivedInterface : IBaseInterface
{
void Method2();
}
Inheriting an interface can be implemented in the following way:
Example
// define a base interface
interface IBaseInterface
{
void Method1();
}
// Define a derived interface that inherits from the base interface
interface IDerivedInterface : IBaseInterface
{
void Method2();
}
// Class that implements 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 the MyClass class
MyClass obj = new MyClass();
// Call method inherited from base interface
obj.Method1();
// Call method newly added by 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 result is:
Method1 implementation Method2 implementation
C# Multiple Inheritance
Multiple inheritance refers to the ability of a class to inherit behaviors and characteristics from more than one parent class at the same time. 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:
Example
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:
总面积: 35 油漆总成本: $2450other extensions