Introduction

This little article is intended for rank .NET newbies who are making their
first attempts at C# programming. I assume that they have done some elementary
C++ programming and know what classes and member functions are. Using a few
simple code snippets we'll see how C# supports inheritance and polymorphism.

Inheritance & Polymorphism

When you derive a class from a base class, the derived class will inherit all
members of the base class except constructors, though whether the derived class would be able to
access those members would depend upon the accessibility of those members in the
base class. C# gives us polymorphism through inheritance. Inheritance-based
polymorphism allows us to define methods in a base class and override them with
derived class implementations. Thus if you have a base class object that might
be holding one of several derived class objects, polymorphism when properly used
allows you to call a method that will work differently according to the type of
derived class the object belongs to.

We have an object of type Animal, but it references an object of type
Dog.
Thus you can see the base class constructor getting called first followed by the
derived class constructor. Now we call Talk() and find that the method that's
executed is the base class method. That's not surprising when you consider that
the object was declared to be of the base type which in our case is Animal. Now
when we call Sing(), we find that the derived class method has got called. This
is because in the base class the method is prototyped as public virtual void
Sing() and in the derived class we have overridden it by using public override void Sing().
In C#, we need to explicitly use the override keyword as opposed to
C++ where we didn't have to do that. And finally when we call Greet()
the base class method gets called and this is not confusing at all
specially since the derived class has not even implemented the method.

Okay, here everything came out as expected. No rude surprises there. The fact
that we could invoke the Greet() method is proof of inheritance in
C#, not that anyone had any doubts to begin with I guess. Now take a look at
this new class we'll be using as a base class for some other classes.

Well, that went fine too. Thus if you have overloaded methods, you can mark
some of them as virtual and override them in the derived class. It's not
required that you have to override all the overloads. Now I want to demonstrate
some stuff on overloaded constructors. For that we'll use the following base
class.

The base class had two overloaded constructors. One that took zero arguments
and one that took an int. In the derived class we only have the zero argument
constructor. Constructors are not inherited by derived classes. Thus we cannot
instantiate a derived class object using the constructor that takes an int as
parameter. As you will deduce from the output we got, the base class constructor
that called was the default parameter-less constructor. Now take a look at this
second derived class.

class DundasSoftware : Software
{
//Here I am telling the compiler which
//overload of the base constructor to call
public DundasSoftware(int y) : base(y)
{
Console.WriteLine(m_x);
}
//Here we are telling the compiler to first
//call the other overload of the constructor
public DundasSoftware(string s, int f) : this(f)
{
Console.WriteLine(s);
}
};

Here we have two constructors, one that takes an int and one
that takes a string and an int. Now lets try some code
out.

There, now that you've seen how it came out, things are a lot clearer I bet.
You can use the this and base access keywords on other
methods too, and not just on constructors.

Conclusion

This article does not discuss interfaces. At least not in it's current
version. I'll probably add the usage of interfaces in the next update. But for
now, I recommend that you read up on interfaces from elsewhere.

Share

About the Author

Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 16 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (14 consecutive awards as of 2015).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Comments and Discussions

When we are inheriting the Base class means we are able to access all the methods and variables in a base class. That means when we create the instance of the derived class it automatically creates the instance of base class.