Hello everyone here! The Last post was about how to create your first class in C#.NET including a simple explaination of Oriented Object Programming(OOP) concept. This tutorial will be about the inheritance between classes.

What is inheritance?

In OOP, the inheritance is a concept that make a class inheriting methods and attributes from another class. The original class is called the base class, from which inherits the other classes, the derived ones. Multiple inheritance doesn’t exists in C#.NET, one class can’t inherits from multiple classes. So fortunatly interfaces are here to solve this problem. Cheers!

In life, a Student is a Person. But a person is not necessary a student. He can be a doctor, a teacher, a driver or whatever because it’s the general case. Student is a paticular type of the person. Or else, a duck is a bird, but bird is not necessary a duck.

If we go far in C#.NET, and especially in GUI forms, a Form class is the base class of all Winform Objects we create in a Windows Form Application Solution. It contains all attributes all Forms such as Title, BackColor, ForeColor… For example, I create a new Form called FormMenu in my solution. This class inherits from Form class all attributes such as Title, ForeColor and whatever, and also all methods such as ShowDialog(), Show()… So, FormMenu is a Form, but Form is not necessary a FormMenu. It can be whatever.

In OOP, we call this general case object a Base class and the other, the particular, a derived class. We will create 2 differents classes Person and Student:

class Person {
protected string FirstName { get; set; }
protected string LastName { get; set; }
protected int Age { get; set; }
public Person(string FirstName, string LastName, int Age) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Age = Age;
}
public Person() {
this.FirstName = "Jane";
this.LastName = "DOE";
this.Age = 24;
}
public virtual string speak() {
return "Hello! My name is "+this.FirstName+" "+this.LastName+" and I'm "+this.Age+" old.";
}

This class above is the base class from which all other classes inherit. It contains 3 protected attributes: FirstName(string), LastName(int) and Age(int), 2 constructors and one virtual string method speak(). Virtual Methods are an overridable and inheritable methods redefined in derived classes. These methods have the same signature in the derived classes and are preceded by the keyword override. In derived classes, virtual methods can be extended by adding much more codes and declarations.

Now, we will create a derived class called Student :


class Student : Person {
private string Code {get; set;}
public Student() : base() {
this.Code="A092017";
}
public override string speak() {
return base.speak()+ " and my Student Code is "+this.Code+"\n";
}
}

The class Student inherits from the class Person and has one private attribute Code(string), one constructor and one overrided string method speak().

Below, the class diagram of the 2 classes Person and Student and the inheritance relationship between them:

inheritance1

I’m a Person. I have a First name, a Last Name and an age and I can speak. I can be a student, a doctor, a director, or whatever.

I’m a Student and I’m also a Person. More than having a First Name, a Last Name and an Age, I have a Code which identified me at university. I can also speak, because I’m a person. 🙂

You see? Inheritance concept in OOP is very simple to understand. Now, we will create new Person and Student object in main class then press F5 key to debug the program(after making sure that there’s 0 error):


class Program {
public static void main(string[] args) {
Console.Title = "CSharp Tutorial - Inheritance Example";
//Creating a new Person
Person person = new Person();
//Creating a new Student
Student student = new Student();
//Calling the method speak() from Person class.
Console.WriteLine(person.speak());
//Calling the method speak() from Student class.
Console.WriteLine(student.speak());
//Waiting for a key Input. Useful to stop auto-closing console.
Console.ReadKey(true);
}
}

VS-5

Question? Write it below