By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

What is the difference between polymorphism and inheritance...

P: n/a
Can anyone explain briefly what is the difference between inheritance
and polymorphism?

i read and seem to forget it again and again...

Can anyone along with good examples of c# explain the fundanmental
concept so i can remember it forever as it is a common interview
question....

Thanks in advance

May 25 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
abstract class Parent
{
}

class Child1 : Parent
{
}

class Child2: Parent
{
}

Child1 and Child2 are examples of inheritance.

///////////////////////////////////////

Parent p1 = new Child1();
Parent p2 = new Child2();

This is an example of polymorphism. Class Parent is "polymorph" - it can
take multiple forms.

--
http://www.rackaroo.com
"weird0" <am********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
Can anyone explain briefly what is the difference between inheritance
and polymorphism?

i read and seem to forget it again and again...

Can anyone along with good examples of c# explain the fundanmental
concept so i can remember it forever as it is a common interview
question....

Thanks in advance

May 25 '07 #2

P: n/a
On Fri, 25 May 2007 11:13:51 -0700, weird0 <am********@gmail.comwrote:
Can anyone explain briefly what is the difference between inheritance
and polymorphism?
Briefly? Heh...okay. I'll try.

Inheritance: when one object inherits functionality implemented by
another. This allows you to create new objects that share base
functionality with other objects, while implementing their own new,
additional functionality.

As an example: in .NET, all classes derived from Control inherit the
Control.Text property. They don't need to implement that property
themselves, as it exists in the base class and is already done for them.

Polymorphism: when functionality defined in some base object can vary in
implementation depending on the actual object instance and what derived
class it actually is. This allows you to create base objects that define
and even implement a basic contract (or "interface") but allow derived
classes to change the way that contract is implemented.

As an example: in .NET all classes derived from Control inherit the
OnPaint() method. They only have to implement a new vesion of that method
if they want to draw differently from the base class, but more importantly
*if they do implement a new version of that method*, then a caller with a
reference to that class does not need to know that it's the derived
class. It can call the base Control.OnPaint() method via a Control-typed
reference (eg "Control ctl; ctl.OnPaint()") and the derived class's
implementation will still be the one that's called (or via any other base
class in the inheritance chain for that instance, for that matter).

As a more concrete example of the above: let's suppose you wanted to
inherit from the PictureBox class, and you wanted to take advantage of the
PictureBox's OnPaint implementation, but add to that a line drawn
diagonally through the image. You could create a new class that inherits
from PictureBox, and which implements a new OnPaint() method that simply
calls the base OnPaint() method, and then draws a diagonal line across the
control. In this case, you are taking advantage of both inheritance of an
implementation as well as the polymorphism of the base Control class.

You might as well ask about interfaces too. :) They are like abstract
classes in C++, where you would have one or more virtual methods without
an implementation ("pure virtual"), requiring all inheritors to implement
the method. The main difference being that an interface cannot have any
implementation defined in it, while a C# class cannot have "pure virtual"
methods. C# makes a clear delineation between the two, while C++ allows
you to have an abstract class that still implements some functionality.

Pete
May 25 '07 #3

P: n/a
Hey Ashot!
Wont the compiler give an error since u instantiated the object of
abstract class Parent.?

Parent p1 = new Child1();
Parent p2 = new Child2();

An abstract class is one whose objects cannot be instantiated.

May 25 '07 #4

P: n/a
weird0 <am********@gmail.comwrote:
Hey Ashot!
Wont the compiler give an error since u instantiated the object of
abstract class Parent.?

Parent p1 = new Child1();
Parent p2 = new Child2();

An abstract class is one whose objects cannot be instantiated.
The only objects created are instances of Child1 and Child2. They
derive from Parent, but no plain Parent objects have been created.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 25 '07 #5

P: n/a
Nope, it won't give you any errors. As you see, there weren't any abstract
methods defined in Parent, which were not implemented in derived classes. So
the compiler will do just fine.
--
http://www.rackaroo.com
"weird0" <am********@gmail.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
Hey Ashot!
Wont the compiler give an error since u instantiated the object of
abstract class Parent.?

Parent p1 = new Child1();
Parent p2 = new Child2();

An abstract class is one whose objects cannot be instantiated.

May 25 '07 #6

P: n/a
On Fri, 25 May 2007 12:03:48 -0700, weird0 <am********@gmail.comwrote:
Hey Ashot!
Wont the compiler give an error since u instantiated the object of
abstract class Parent.?

Parent p1 = new Child1();
Parent p2 = new Child2();
But he didn't. He instantiaed instances of the derived objects, Child1
and Child2.
An abstract class is one whose objects cannot be instantiated.
Among other things, yes. But the code doesn't instantiate a Parent
instance.

Pete
May 25 '07 #7

P: n/a
Hello weird0,

http://en.wikipedia.org/wiki/Polymor...ed_programming
http://en.wikipedia.org/wiki/Inheritance

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

wCan anyone explain briefly what is the difference between
winheritance and polymorphism?
w>
wi read and seem to forget it again and again...
w>
wCan anyone along with good examples of c# explain the fundanmental
wconcept so i can remember it forever as it is a common interview
wquestion....
w>
wThanks in advance
w>
May 25 '07 #8

P: n/a
Nice info on inheritance!

Here's some on polymorphism:
http://www.webster.com/dictionary/polymorphism

--
http://www.rackaroo.com
"Michael Nemtsev" <ne*****@msn.comwrote in message
news:a2***************************@msnews.microsof t.com...
Hello weird0,

http://en.wikipedia.org/wiki/Polymor...ed_programming
http://en.wikipedia.org/wiki/Inheritance

---
WBR, Michael Nemtsev [.NET/C# MVP]. My blog:
http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

wCan anyone explain briefly what is the difference between
winheritance and polymorphism?
wwi read and seem to forget it again and again...
wwCan anyone along with good examples of c# explain the fundanmental
wconcept so i can remember it forever as it is a common interview
wquestion....
wwThanks in advance
w>

May 25 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.