Understanding Inheritance in C# – Part 1

Title

Understanding Inheritance in C# – Part 1

What is inheritance?

Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.
The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by using inheritance.

Golden Rule of inheritance

“Child is parent but parent is not a child”, in other words we can say,
“Dog is an Animal, but animal is not a dog.”

The below example will prove this rule.

The following are the types of inheritance in C#.

types of inheritance

 

 

 

 

 

 

 

The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.

Base class: is the class from which features are to be inherited into another class.
Derived class: it is the class in which the base class features are inherited.

Single Inheritance

single

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

It is the type of inheritance in which there is one base class (i.e. parent class) and one derived class (i.e. child class)

Parent Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Animal
    {
        public void showData()
        {
            Console.WriteLine("This is Class Animal");
        }
    }
}

Child Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Dog : Animal
    {
        new public void showData()
        {
            Console.WriteLine("This is class Dog");
        }

    }
}

Main Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Animal anm = new Animal();
            Dog dg = new Dog();

            anm.showData();
            dg.showData();

            ((Animal)dg).showData();
            ((Dog)anm).showData();
        }
    }
}

Here I’ve showData() function in both parent and child class (i.e. function overriding). If we create a parent class object and try to call showData() function it will call the function which is defined in parent class and same way if we create a child class object and try to call showData() fucntion, it will call the function which is defined in child class.

But if you see below two piece of code

((Animal)dg).showData();
((Dog)anm).showData();

Here I’ve have typecast the child class object with parent class and now if we call
function using child class object, now it will access parent class function
(i.e child is parent), this typecasting makes no significance in this case but I’ve
done this just inorder to show that this is possible and can be used in some special
cases.
On the other we have forced typecast parent class object with child class and now
we are trying to call child class function using parent class object,
what will happen?
It will throw an exception. (i.e. parent is not a child)

error


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar