Inheritance In C#

In our previous post we learned about this keyword and why it is used. Now in this post let's discuss about inheritance which is a very interesting and useful concept of programming. We will see what is inheritance and how it is used in programming with the help of simpleexample programs. So let's get started

Inheritance in csharp with example




What Is Inheritance?


Inheritance in layman terms is getting (a quality, characteristic) genetically from one's parents or ancestors. So similarly in programming inheritance comes into picture when there's a common class with useful properties and methods that other classes can use. So other classes just inherit those properties and methods from the common class and thus saving the effort of declaring the same methods & properties inside themselves. Consider the below example

public class ParentClass
{
    public int Property1{get;set;}
    public string Property2 { get; set; }

    public void Method1()
    {
 //Code for method
    }
    public void Method2()
    {
 //Code for method
    }
}   

public class ChildClass : ParentClass
{
    public int Property3 { get; set; }
}

Now in this example there's a class named as ParentClass, it has some properties & methods which might be useful for other class as well. Now there's a class named ChildClass which needs these properties & methods. So now ChildClass will inherit these characteristics from ParentClass using inheritance.

To inherit from a class we just have to use ':' colon after the name of the child class followed by the name of the class from which we want to inherit. Here we can see ChildClass : ParentClass.

The class which inherits is called derived class and the one which is the parent class is called base class. Here ParentClass is base class & derived class is ChildClass.

Now as ChildClass has inherited from ParentClass, all the fields, properties and methods of parent class are now available to ChildClass. So if we create an instance of ChildClass we can see all the things available from base class. Consider the below image

Accessing inherited properties of base class from derived class, inheritance example


Multiple Inheritance


Suppose in a scenario we want properties of two different classes, to be accessed in a third class. So easy way we would think of is inheriting directly from both the classes using comma. But there is a big roadblock. In inheritance we can't inherit from two classes, which means that a class can't have multiple base classes. The compiler throws error if we inherit from multiple classes.

public class Class1
{
    public int Property1{get;set;}
}   

public class Class2
{
    public int Property2 { get; set; }
}

//Compiler throws error for below example
public class Class3: Class1, Class2
{
    public int Property3 { get; set; }
}

So to overcome this roadblock we can do one thing, which is multiple inheritance. We can't directly inherit two classes but if we inherit Class1 from Class2 and then Class2 from Class3 then Class3 can access all the public properties from Class1 & Class2. Consider the below example


public class Class1
{
    public int Property1{get;set;}
}   

public class Class2 : Class2
{
    public int Property2 { get; set; }
}
//Now Class3 has inherited Property1, Property2 using Multiple Inheritance
public class Class3: Class2
{
    public int Property3 { get; set; }
}

Here in this example if we create instance of Class3 then we will be able to access all Property1, Property2 & Property3 from the object of Class3 while Class2 object only have access to Property1 & Property2.


That's how inheritance works. There are various aspects related to Inheritance like Abstract Classes, Interfaces & Access Modifiers(To limit the accessibility of fields and properties of a class). We will be covering these topics in our upcoming posts. Stay tuned.

Let us know if you have any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)

Comments

Popular posts from this blog

Just Maths - Views, View Groups and Layouts

Create Android Apps - Getting Started

Just Maths - Android Activities and Layouts