what's difference b/t polymorphism andinheritance

Recommended Answers

All 7 Replies

inheritance is a Is A relation Ship between Classes
Polymorphism means many forms
or single interface multiple implimentations
is correct???

tell me some more differrence
plz
plzzzzzzzzzzz??????

Remember that Google can be a very good friend. Example, among a plethora of others.

In OOP terminology, polymorphism is the ability for sub-classes to extend or even change the behavior they inherit from their parent classes. For example, if you have a class Animal, which has an abstract method named speak():

public abstract class Animal {
    public abstract void speak();
}

Then you can instantiate the behavior in the sub-classes Dog and Cat thusly:

public class Dog: Animal {
    public override void speak() {
        Console.WriteLine('Woof');
    }
}

public class Cat: Animal {
    public override void speak() {
        Console.WriteLine(Meow');
    }
}

If you then create an Animal variable, and assign it a Dog() object, the object will respond to the speak() method with "Woof".

what's difference b/t polymorphism andinheritance

Polymorphism is an optional feature of inheritance. Your question suggests that you think they're independent, which they're not. Without inheritance (or a significant amount of hackish workarounds), polymorphism doesn't occur.

That's assuming of course that we're talking about polymorphism through virtual member functions rather than polymorphism through templating or overloading. I think that's a safe assumption though, as most people mean polymorphism through inheritance when they say "polymorphism".

It may also be useful to point out that polymorphism is a characteristic of your design, whereas inheritance is a feature of your development system.
For exammple when in winforms, you drag a button and a texbox on your form, you experience polymorphism. This is made possible because both textboxes and buttons inherit from controls. In fact what achieves polymorphism is the fact that different objects implement a common interface. Inheritance is one of the features that provide this, but it's not the only one, and what's more, inheritance in itself does not warrant polymorphism.

hi,
In c# polymorphism, which means many forms.
it contains overloading and overriding.
overloading in the sence complile time polymorphisim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.