473,769 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20612
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********@gma il.comwrote in message
news:11******** **************@ o5g2000hsb.goog legroups.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********@gma il.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********@gma il.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.co m>
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********@gma il.comwrote in message
news:11******** **************@ w5g2000hsg.goog legroups.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********@gma il.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.co mwrote in message
news:a2******** *************** ****@msnews.mic rosoft.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
2848
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 use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
11
5722
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 difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public vs. private. I have always just used structs as a minimal class, as that is the way
12
7057
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 I've ended up using successfully for certain kinds of inheritance and polymorphism, and some that have not worked out so well. Let's start with obvious things that turn out not to work well: 1. Use interface classes and "Implements" for...
19
1815
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 discuss how the people thinking about OOP :-) Regards,
6
6080
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 using System; using System.Collections.Generic; using System.Text;
5
4122
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
4
3925
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
18
3864
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 different objects to respond to the same request message in their own unique way" I thought that it was: "the ability of same object to respond to different messages in
11
2977
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++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.