472,342 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

What is the difference between polymorphism and inheritance...

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
8 20458
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is...
19
by: G.Ashok | last post by:
Hi All, What is your weightage of the 3 characteristics of Object-Oriented Programming i.e. Inheritance, Encapsulation and Polymorphism. for a...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from...
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
4
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however,...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.