Inheritance
Unless this is your first time at object oriented programming, you will have heard a lot about Reusability and Extensibility. Reusability is the property of a module (a component, class or even a method) that enables it to be used in different applications without any or little change in its source code.
Extensibility of a module is it's potential to be extended (enhanced) as new needs evolve. Reusability in Object Oriented programming languages is achieved by reducing coupling between different classes, while extensibility is achieved by sub-classing. The process of sub-classing a class to extend its functionality is called Inheritance or sub-typing.
The original class (or the class that is sub-typed) is called the base, parent or super class. The Class that inherits the functionality of the base class and extends it in its own way is called the sub, child, derived or inherited class.
For example:
In the figure above, we have used a UML (Unified Modeling Language) class diagram to show Inheritance. Here, Shape is the base class while Circle, Rectangle and Curve are its sub-classes. A base class usually has general functionality, while Circle, Rectangle and Curve are its sub-classes. A base class usually has general functionality, while possess specific functionality. So, when sub-classing or inheriting, we go 'from specialization to generalization'.
If a class B (sub class) inherits a class A (base class), then B would have a copy of all the instance members (fields, methods, properties) of class A and B can access all the members (except for the private members) of class A. Private members of a base class do get inherited in a sub-class, but they can not be accessed by the sub-class. Also, inheritance is said to create a, 'type of' relationship among classes which means sub-classes are a type of base class. (If you are not getting the idea, don't worry, things will get clearer when we implement inheritance in the following sections)
Inheritance in C#
Before we go on to implementation, here are some key-points about inheritance in C#
C#, like Java and contrary to C++, allows only single class inheritance, Multiple inheritance of classes is not allowed in C#
The Object class defined in the System namespace is implicitly the ultimate base class of all the classes in C# (and the .NET framework)
Inheritances in C# can inherit more than one interface
So, multiple inheritance of interfaces is allowed in C# (again similar to Java). We will look at interfaces in detail in the coming web lessons page, Structures (struct) in C# can only inherit (or implement) interfaces and can not be inherited.
Author's Note: Technically, a class inherits another class, but implements an interface. It is technically wrong to say that class A inherits interface B, it should be like, 'class A implements interface B'. Although, many write-ups don't follow this, I would recommend using the word 'inherits' only where it makes sense.
Implementing inheritance In C#
C# uses the colon ':' operator to indicate inheritance. Suppose we have a class Student with the fields registrationNumber, name and dateOfBirth, along with the corresponding properties. The class also has a methodGetAge() which calculates and returns the age of the student.
class Student
{
// private Fields
private int registrationNumber;
private string name;
private DateTime dateOfBirth;
// Student Constructor
public Student()
{
Console.WriteLine("New student created. Parameterless constructor called...");
}
public Student(int registrationNumber, string name, DateTime datOfBirth)
{
this.registrationNumber = registrationNumber;
this.name = name;
this.dateOfBirth = dateOfBirth;
Console.WriteLine("New Student Created. Parameterless constructor called... ");
}
// Public Properties
public int RegisterationNumber
{
get { return registrationNumber; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public DateTime DateOfBirth
{
get { return dateOfBirth;}
set { dateOfBirth = value; }
}
// public Method
public int GetAge()
{
int age = DateTime.Now.Year - dateOfBirth.Year;
return age;
}
}
The Student class above is very simple. We have defined three private fields, their accessor properties and one method to calculate the age of the student. We have defined two constructors; the first one takes no parameters and the other takes three parameters. Note that we have only defined the get { } property of registrationNumber since we do not want the user of the Student class to change registrationNumber once it has been assigned through the constructor.
Also not that we did note make the GetAge() a property, but a method, The reason for this is that properties are generally supposed to be accessors for getting/setting the values of fields and not for calculating/processing the data. Hence, it makes sense to declare GetAge() as a method.
Now, let us declare another class named SchoolStudent that inherits the Student class, but with additional members like marks of different subjects and methods for calculating total marks and percentage.