What is Inheritance?

Inheritance is considered as the second pillar of Object Oriented Programming(OOP).

Inheritance is a way for a class or an object to automatically acquire properties and behaviors of another class. The class/object which inherits is called the child class/subclass/derived class. The class whose properties and behaviors are passed on (to be inherited) is called the base class/parent class.

A real life analogy is the talents we inherit from our parents or our family line. Several abilities like singing or athletic capabilities are passed on from our parents or grandparents or beyond that. Same analogy is applicable in OOP as well. A class can access the properties and behaviors of its immediate base class or the parent of the base class and so on provided correct access is specified.

A simple example would be an Animal class. Since all animals breath, a root behavior can be Breath. A child class can be Mammals. Classes like Lion,Tiger etc can be derived from Mammals. So a Lion would inherit the behavior Breath from Animal class via Mammal.

So in the example design, Mammal inherits Breath function. Mammal can use that breath behavior or may override the behavior with its own implementation, provided Animal class implemented an override version. Fish class also derives from Animal. Since Fish breathes in a different way it may want to definitely provide its own implementation. (The above design is by no means complete or accurate.)



    public virtual void Breath()

    {    //complicated breathing code 


    private void Play()


     //Complicated code


    protected void  Eat()


     //Complicated code



The “virtual” keyword before Breath allows derived classes to override base class implementation and have some new implementation. So in our example our Fish class can override it and implement its own Breath since Fish breath from water using its gills. The behavior Eat can be used by Lion and Fish, however they cannot be overriden. The behavior Play can NOT be used by any of the child classes, because of the "private" keyword.

The keyword “public”,"private" etc dictates who can access it. It’s called access specifier. There are 4 different access specifiers in C#. They are

  • public
  • Anybody can access it.

  • private
  • Nobody can access it. Usually private methods are used for some internal operations which may not make much sense to the outside world.

  • protected
  • A child class can access protected members. But anybody below that rank ( grand children or great grand childer etc) cannot access it.

  • internal
  • Access is limited to the same assembly.

OK! It's all great and fantastic! How does it help?

The main advantage of inheritance is code reusability. You can reuse the Breath behavior in the above example in any of the child classes. The second advantage is that you can group related objects and work with them which makes programming much simpler and easier. In procedural based languages( before OOP era) there was no such grouping possible, which forced the programmers to write a lot more code. Inheritance when combined with polymorphism makes OOP very powerful.

A real world example would be a Form class in C#. When you create a form, it is derived from the .Net class Form. All the drawing code and all the event handling etc are done by Microsoft programmers. You are just reusing them. Read an article on polymorphism on this site.

If you want to be an object oriented programmer you have to master these concepts. You have to breath in and breath out the three fundamental pillars of OOP.

We offer one on one C# training,SQL training,WCF training,WPF training,WinForms training, MVC training, ASP.NET training, Ajax & Javascript training. Please see the contacts page

Grady Booche's Object Oriented Analysis and Design With Applications is great book on OOP.