What is the difference between polymorphism and inheritance...

W

weird0

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
 
A

Ashot Geodakov

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.
 
P

Peter Duniho

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
 
W

weird0

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.
 
J

Jon Skeet [C# MVP]

weird0 said:
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.
 
A

Ashot Geodakov

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.
 
P

Peter Duniho

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
 
M

Michael Nemtsev

Hello weird0,

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_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

w> Can anyone explain briefly what is the difference between
w> inheritance and polymorphism?
w>
w> i read and seem to forget it again and again...
w>
w> Can anyone along with good examples of c# explain the fundanmental
w> concept so i can remember it forever as it is a common interview
w> question....
w>
w> Thanks in advance
w>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top