473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 20569
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 extend. In other part of the framework, I heavily...
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 more today, and found that there is very 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 non-existent. This little essay is about some patterns...
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 total of 100% Please quote your values and let's...
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 Programming C#: #region Using directives ...
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, polymorphism is defined as: "the ability of two...
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 polymorphism is such an integral part of C++,...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.