C# Inheritance
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define one class based on another, which makes creating and maintaining applications easier. It also helps reuse code and save 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 thebase class, and this new class is called thederived class。
The idea of inheritance implements an is-a (IS-A) relationship. For example, a mammal is-a (IS-A) is an animal, a dog is-a (IS-A) is a mammal, therefore a dog is-a (IS-A) is an animal.
Base Classes and Derived Classes
A class can inherit from another class, called the base class (parent class) and derived class (child class).
C# does not support multiple inheritance of classes, but supports multiple inheritance of interfaces; a class can implement multiple interfaces.
**In summary:** a class can inherit multiple interfaces, but can only inherit from one class.
The syntax for creating a derived class in C# is as follows:
<
access modifiers
class
<
base class
{
...
}
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 use the base keyword to call the base class's constructors and methods.
Example
class
BaseClass
{
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
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 code above is compiled and executed, it produces the following result:
总面积: 35
Initialization of the Base Class
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
.
ReadLine
(
)
;
}
}
}
When the code above 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 a derived interface inherits all the members of the base interfaces.
A 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
(
)
;
}
Inheritance of interfaces can be implemented in the following way:
Example
using
System
;
// Define a base interface
interface
IBaseInterface
{
void
Method1
(
)
;
}
// Define a derived interface that inherits from the base interface
interface
IDerivedInterface
:
IBaseInterface
{
void
Method2
(
)
;
}
// A 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 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 behaviors and characteristics from more than one parent class at the same time. It is opposed to single inheritance, which means a class can only inherit from 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 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 code above is compiled and executed, it produces the following result:
总面积: 35
油漆总成本: $2450