Inheritance in C#.

Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.

Consider the following classes:

class Friend
{
    public Friend()
    {
        Console.WriteLine("Friend constructor");
    }
    public void Greet()
    {
        Console.WriteLine("Friend Hello");
    }
    public void Talk()
    {
        Console.WriteLine("Friend talks");
    }
    public virtual void Sing()
    {
        Console.WriteLine("Friend sing");
    }
}
class Dawg : Friend
{
    public Dawg()
    {
        Console.WriteLine("Dawg constructor");
    }
    public new void Talk()
    {
        Console.WriteLine("Dawg talks business");
    }
    public override void Sing()
    {
        Console.WriteLine("Dawg rapping");
    }
}

Create console application and try this out:

Friend f1 = new Friend();

f1.Talk();
f1.Sing();
f1.Greet();

//Output
Friend constructor
Friend talks business
Friend sing Friend says Hello

Everything looks as expecting. Let’s add some come:

Friend f2 = new Dawg();

f2.Talk();
f2.Sing();
f2.Greet();

//Output
Friend constructor
Dawg constructor
Friend talks business
Dawg rapping
Friend says Hello

Here we have object of type Friend, but it references an object of type Dawg. You can see that the base class constructor is called first and then called derived class constructor. Now we call Talk() method and find out that that’s executed is the base class method. That’s because object was declared to be of the base type which is the Friend. Then we call Sing() and we see that derived class method is called. This is because the base class method created as virtual and it is overridden  in the derived class. Finally, we call Greet()  base class method and it’s called since it is the derived class has not implemented this method.

Let’s see what overloaded constructors mean. Consider following classes:

 

class Team
{
    protected int salaryCap;
    public Team()
    {
        salaryCap = 80;
    }
    public Team(int cap)
    {
        salaryCap = cap;
    }

}
class Bulls : Team
{
    public Bulls()
    {
        Console.WriteLine(salaryCap);
    }
}

Now, try this code out:

Bulls b = new Bulls();

//Output
80

The base class has two constructors. One take zero arguments and another one takes int. In the derived class we only have the constructor with zero arguments. Constructors are not inherited by derived classes. So we cannot instantiate a derived class object using the constructor that takes int as parameter. So in order to use constructor of a base class we need to change the code of our derived class:

class Bulls : Team
{
    public Bulls(int cap):base(cap)
    {
        Console.WriteLine(salaryCap);
    }

    public Bulls(int cap, bool exceptions):this(cap)
    {
        Console.WriteLine(exceptions.ToString());
    }
}

Bulls b = new Bulls(90);
// Output
// 90

Bulls b2 = new Bulls(75, true);
// 75
// True

Here you can see, how we can use constructor of the base class, using the this and base access keywords.
Other topic related to inheritance is interfaces. We will discuss it later and also compare inheritance from abstract class and interface.

Share this post:Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0Share on Reddit0Email this to someoneDigg this