Implementation Inheritance
Implementation inheritance is when a class derives from another base class, inheriting all the base class's members. To add new members to a class, you can define another class that derives from the existing base class. Using implementation inheritance, the new derived class inherits all of the implementation provided in the base class.
To understand how inheritance works in C#, define a simple class as follows:
public class Shape {
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}
Here, the Shape
class contains two properties and a single method. By itself, this class does not specify a particular shape, but it does assume that a basic shape contains length and width. It also assumes that the perimeter of a shape is simply double the sum of its length and width.
Using this base class, you can define other shapes such as square, rectangle, and circle. Let's start with the rectangle shape. Using Shape
as the base class, you can define a Rectangle
class (a derived class because it derives from the Shape
class) by inheriting from the Shape
class, like this:
public class Rectangle : Shape {}
In C#, you use the colon (:
) operator to indicate that a class inherits from another class (known as the base class). This example reads: 'The Rectangle
class inherits from the Shape
class.' This means that whatever members the Shape
class has are inherited by the Rectangle
class. (In this example, the Rectangle
class has no implementation; that will be added in the next few sections.)
C# supports only single-class inheritance, which means that a class can inherit directly from only one base class. If you do not specify the base class, the C# compiler assumes that it is inheriting from the System.Object
class. Because the Shape
class did not specify who it is inheriting from, it is equivalent to:
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}
To use the Rectangle
class, you instantiate it as you would other classes:
Rectangle r = new Rectangle();
Because the Rectangle
class inherits all the members of the Shape
class, you can access its members as if they are defined within the Rectangle
class itself:
r.length = 4;
r.width = 5;
Console.WriteLine(r.Perimeter()); //---18---
Abstract Class
The Shape
class does not specify a particular shape, and thus it really does not make sense for you to instantiate it directly, like this:
Shape someShape = new Shape();
Instead, all other shapes should inherit from this base class. To ensure that you cannot instantiate the Shape
class directly, you can make it anabstract
keyword:
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}
Once a class is defined as abstract, you can no longer instantiate it directly; the following is now not permitted:
//---cannot instantiate directly---
Shape someShape = new Shape();
The abstract
keyword indicates that the class is defined solely for the purpose of inheritance; other classes need to inherit from it in order to have objects of this base type.
Abstract Methods
Besides making a class abstract by using the abstract keyword, you can also createShape
class as an example, you can now define an abstract method called Area()
that calculates the area of a shape:
public abstract class Shape {
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}