Wednesday, June 20, 2012


Source Code Inheritance Project

Inheritance is used by a class to inherit another class its methods, properties and global variables. You can then add more methods, properties and global variables to the class that inherited the other class, so it can do the same and more.

If inheritance did not exist, then there would be a lot of duplicate methods. Let's say that guests, members and administrators can post a comment on your website. All those classes would then require a method called "makeComment(string commentText)". If you want to make a change to that method, then you will have to change the method in the guest, member and administrator class.

With inheritance you can make one class called user and let all the other classes inherit the user class. That way you only have to make changes to the makeComment method in the user class. Some people might think: "But if I just change the makeComment method once and copy paste it in the other classes, I won't need inheritance!". My answer to that: programmers should be so incredibly lazy, that even copy pasting code should look like too much trouble.

Let's start already by creating a new console application called "Inheritance Tutorial". Add a new class to the project called "Woman".

Add the following code to the Woman class:
private string _name;
public string Name
get { return _name; }
set { _name = value; }
private int _age;
public int Age
get { return _age; }
set { _age = value; }
The Woman class now has two properties called Name and Age.

Add a new class called "Mother" and add the code " : Woman" after the text "class Mother", so it will look like the following:
class Mother : Woman
This makes the Mother class inherit all the global variables, methods and properties from the Woman class. Mother now has the properties Age and Name, even though the properties are not visibly there in the Mother class file.

You can only inherit a maximum of one class. 

People in the IT world like to give names to everything, so they named the class that inherits another class the derived class and they named the class that is inherited the base class. Mother is the derived class and Woman is the base class in your software.

Type the following in your Program class' main method to test if the inheritance's a success:
Mother mother = new Mother();
mother.Age = 12;
mother.Name = "Mom";
Press F5 to see it in action. As you can see, Mother now has the properties Age and Name. Next I want to show you that Mother can have the same methods, global variables and properties as Woman and more.

Go back to your Mother class and add the following method:
public void TellBedtimeStory()
Console.Write("Once upon a time...");
Now Mother has the methods, properties and global variables it inherited from Woman plus its own method TellBedtimeStory(). 

Go to your Program class and replace the code in its Main method with the following:
Mother mother = new Mother();
Press F5 and you will see that it works like a charm. Last thing I would like to show is that when you initialize a Woman object, that you can't call the "TellBedtimeStory()" method from it.

Add the following code to your Program class' Main method:
Woman woman = new Woman();
Press F5 and you will see that it won't work, because Woman does not have that method.

For some people this tutorial must have been a little too detailed. That's because I wanted to show exactly where inheritance starts and ends.

Source Code Inheritance Project

>> Interfaces


  1. Inheritance is great. It's there in CSS as well. Makes sure you don't have to repeat yourself a thousand times over and when you want to change something don't have to change all its instances in every single class.

    1. Yup, CSS is a great example. I think the whole idea of CSS is based around inheritance, but that just might be me.