Click here to Skip to main content
6,293,171 members and growing! (11,750 online)
Email Password   helpLost your password?
Languages » C# » Beginners     Beginner

Introduction to inheritance, polymorphism in C#

By Nishant Sivakumar

An elementary introduction to inheritance, polymorphism in C# using simple code snippets
C#, Windows, .NET 1.0, Dev
Posted:9 Oct 2001
Views:309,628
Bookmarked:79 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
108 votes for this article.
Popularity: 8.27 Rating: 4.07 out of 5
3 votes, 4.7%
1
1 vote, 1.6%
2
5 votes, 7.8%
3
17 votes, 26.6%
4
38 votes, 59.4%
5

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.

Consider the following class which we'll use as a base class.

class Animal
{
    public Animal()
    {
        Console.WriteLine("Animal constructor");
    }
    public void Greet()
    {
        Console.WriteLine("Animal says Hello");
    }
    public void Talk()
    {
        Console.WriteLine("Animal talk");
    }
    public virtual void Sing()
    {
        Console.WriteLine("Animal song");
    }
};

Now see how we derive another class from this base class.

class Dog : Animal
{
    public Dog()
    {
        Console.WriteLine("Dog constructor");
    }
    public new void Talk()
    {
        Console.WriteLine("Dog talk");
    }
    public override void Sing()
    {
        Console.WriteLine("Dog song");
    }
};

Now try this code out.

Animal a1 = new Animal();
a1.Talk();
a1.Sing();
a1.Greet();

//Output

Animal constructor
Animal talk
Animal song
Animal says Hello

Okay, that came out just as expected. Now try this code out.

Animal a2 = new Dog();
a2.Talk();
a2.Sing();
a2.Greet();

//Output

Animal constructor
Dog constructor
Animal talk
Dog song
Animal says Hello

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.

Now try the following code out.

Dog d1 = new Dog();
d1.Talk();
d1.Sing();
d1.Greet();

//Output

Animal constructor
Dog constructor
Dog talk
Dog song
Animal says Hello

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.

class Color
{
    public virtual void Fill()
    {
        Console.WriteLine("Fill me up with color");
    }
    public void Fill(string s)
    {
        Console.WriteLine("Fill me up with {0}",s);
    }
};

Now run this code out.

Color c1 = new Color();
c1.Fill();
c1.Fill("red");

//Output

Fill me up with color
Fill me up with red

Okay, that went fine, I'd say. Now let's derive a class from this class.

class Green : Color
{
    public override void Fill()
    {
        Console.WriteLine("Fill me up with green");
    }
};

Now let's try this code out.

Green g1 = new Green();
g1.Fill();
g1.Fill("violet");

//Output

Fill me up with green
Fill me up with violet

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.

class Software
{
    public Software()
    {
        m_x = 100;
    }
    public Software(int y)
    {
        m_x = y;
    }
    protected int m_x;
};

Now we'll derive a class from the above class.

class MicrosoftSoftware : Software
{
    public MicrosoftSoftware()
    {
        Console.WriteLine(m_x);
    }
};

Now try this code out

MicrosoftSoftware m1 = new MicrosoftSoftware();
//MicrosoftSoftware m2 = new MicrosoftSoftware(300); //won't compile


//Output

100

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.

DundasSoftware du1 = new DundasSoftware(50);

//Output

50

DundasSoftware du2 = new DundasSoftware("test",75);

//Output

75
test

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.

History

  • 09 Jul 2002 - Article redone completely, sample project added.
  • 10 Oct 2001 - Article posted [My first article on CP]

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
GeneralNice Artical Pinmemberrk4dotnet2:32 1 Apr '09  
Question.net Pinmembersandik4820:44 4 Jan '09  
AnswerRe: .net PinmvpNishant Sivakumar2:58 5 Jan '09  
GeneralIt has all about Inheritance PinmemberShiv...0:39 27 Aug '07  
Generalmade easy to undertand Pinmemberpallavipatil0121:52 21 Jun '07  
GeneralOutput seems to have an error PinmemberComputerKat9:34 19 Jan '07  
GeneralRe: Output seems to have an error Pinmemberpeter200212:49 10 Mar '07  
Generalquestion about last example Pinmemberdaddion17:52 30 Oct '06  
GeneralRe: question about last example Pinmembertbenda7:23 11 Dec '06  
Generalwhy the new modifier? Pinmemberlogistum12:22 29 Jul '06  
GeneralRe: why the new modifier? Pinmemberberen7770:29 23 Aug '06  
GeneralGood tutorial; one question PinmemberHuline8:38 31 Oct '05  
GeneralRe: Good tutorial; one question Pinmembersikemullivan10:29 21 Dec '05  
Generalexcellent Pinmemberhollyireland11:56 28 Oct '04  
GeneralRe: excellent PinstaffNishant S17:11 28 Oct '04  
GeneralIt helped! Thankx PinmemberKavya Menon3:30 18 Sep '03  
GeneralRe: It helped! Thankx PineditorNishant S4:49 18 Sep '03  
GeneralNice beginners article Nish PinmemberBrian Delahunty9:06 28 Dec '02  
GeneralRe: Nice beginners article Nish Pinmemberleppie9:50 28 Dec '02  
GeneralRe: Nice beginners article Nish PineditorNishant S20:32 28 Dec '02  
GeneralRe: Nice beginners article Nish PinmemberBrian Delahunty2:46 8 Jan '03  
GeneralRe: Nice beginners article Nish PineditorNishant S20:31 28 Dec '02  
GeneralRe: Nice beginners article Nish PinmemberBrian Delahunty2:44 8 Jan '03  
GeneralRe: Nice beginners article Nish PinsussAnonymous3:48 26 May '04  
GeneralA very good article ! PinsussAnonymous5:34 22 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Oct 2001
Editor: Nishant Sivakumar
Copyright 2001 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project