Inheritance
Explains and defines inheritance and how it is used to extend a class.
One of the most important concepts of object-oriented programming is inheritance. As we have seen in the last chapter, inheritance is used to extend a class. When we need a class that has some properties and methods common to a previously defined class, we can inherit the new class from the previously defined class. This way, we need to define only those properties and methods of the new class. The common properties and methods will be inherited from the old class.
To understand this, let us take the example of the `Person' class -
The `Person' class has three fields - name, age and gender. It also contains some properties and methods to access the values of these fields. Now we need a class called `student'. This class will have the fields - name, age, gender and stream and the related properties and methods to work on them.
Notice that both classes have some common members. Since we have already created the `Person' class, we can use it for the purpose of inheritance. The class from which another class is inherited is called the base class. In this example, `Person' is the base class. The inherited class is known as the derived class. Therefore, `Student' will become the derived class.
The base class is created normally as we have seen in the last chapter. The syntax for creating the derived class is as follows:
public class derived-class-name : base-class-name
{
//members of the derived class
}
Let us modify the program that we have made in the previous chapter to understand inheritance.
Open the project and from the solution explorer, open the Person.cs file. This file has the definition of the Person class and we are going to use it as our base class. Modify the code as shown below (the code in bold is the modified code):
public class Person { private string name; private int age; private string gender;
public string Name { get { return name; } set { name = value; } }
public string Gender { get { return gender; } set { gender = value; } } public int Age { get { return age; } set { age = value; } }
public void Talk() { Console.WriteLine("Person {0} is talking", this.Name); } }
As we can see above, almost all of the class definition is same except for the statement inside the Talk() method. We will shortly see the reason behind this.
Next, add a new class to the project. To do this, right click on the project name in the solution explorer and select the option Add -> Class... from the menu. Name this class `Student' but do not change the .cs extension. This will open up a new tab in the IDE (Student.cs) with the basic definition of the Student class. Modify this class as shown here:
class Student: Person { private string stream;
public string Stream { get { return stream; } set { stream = value; } }
}
The Student class is derived from the class Person and it has a field called stream and a property to access this field. Note that we have not defined the Name, Age and Gender properties and the Talk() method in the Student class.
Finally, our program file should be modified as follows:
static void Main(string[] args) { Student s1 = new Student(); Console.WriteLine("Enter name:"); s1.Name = Console.ReadLine();
Console.WriteLine("Enter Gender:"); s1.Gender = Console.ReadLine();
Console.WriteLine("Enter Age:"); s1.Age = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Stream:"); s1.Stream = Console.ReadLine();
Console.WriteLine("{0} is {1} years old {2} and has {3} stream", s1.Name, s1.Age, s1.Gender, s1.Stream); s1.Talk(); }
In the Main() method, we have declared an object of class Student. Using this object, we can access the Name, Age and the Gender properties of the base class Person. We can also access the Talk() method. This is possible because of inheritance. Since the Student class is inherited from the Person class, the properties and methods of the Person class is accessible using the object of the Student class.
Here is a sample run of the program:
