To me, the power of inheritance is that you can refer to a generic class and still catch all the sub-classes in a few simple steps. For text-book definitions and examples and exceptions to the rule see here Polymorphism is the ability to take several forms Poly -"many" morphs- "forms" . In OOP , this means that an object (or a reference to an object) can become different forms of objects. Class-based inheritance is related to polymorphism. The same operations that apply to instances of a parent class also apply to instances of its subclasses. AKA "subclass polymorphism". You can also make use of polymorphism without class-based inheritance, by using interfaces. Polymorphism enables re-use by making it possible to implement generic classes that will work not only for a range of existing objects, but also for objects to be added later on.
regards [ October 06, 2003: Message edited by: HS Thomas ]
Originally posted by Pree ty mark: what is uses or advantages of Inheritances and polymorphism in oops.
Polymorphism is a tool for decoupling. Imagine the following simple example:
This code has two responsibilities: knowing *what* to print ("hello world") and *where* to print it (System.out). Now, if we don't want the code to know about *where* to print, we can make use of polymorphism:
This code doesn't know about *where* to print - all it knows is that it gets an object which can print to *somewhere* (could be the console, a file, a database, in memory etc.). http://www.objectmentor.com/resources/articles/dip.pdf shows this on a more elaborate example. In statically typed languages like Java, inheritance is used to implement polymorphism. In dynamically typed ones (Smalltalk, Ruby etc.), inheritance is just a way to share common code between classes. Did that help?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Pradeep Bhat: Many great people say no to inhertinace and say yes to delgation.
I think this is overstated. It's true that inheritance often gets overused by beginners. Still, inheritance can be quite usefull when applied wisely. See the Template Method pattern for a popular example.
I think polymorphism is the great thing OO added to what was known before. The notion that you can be given an object and do some work on it without really knowing what it is is pretty cool. It opens the way to many of the GoF patterns, almost all of which seek to decouple classes from one another to isolate change. Even this idea wasn't completely new, tho. Mainframe COBOL has been calling programs knowing only their names for ages. C and Turbo Pascal and other languages have supported function pointers, too. But OO formalized polymorphism in a wonderful way. Inheritance and implementation of interfaces are two good ways to create polymorphic objects. This is a much bigger benefit of inheritance than merely creating something new by defining how it differs from something old.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jul 11, 2001
Originally posted by Stan James: Even this idea wasn't completely new, tho. Mainframe COBOL has been calling programs knowing only their names for ages. C and Turbo Pascal and other languages have supported function pointers, too.
In fact, even programs (for example device drivers) written in Assembler often use jump tables for exactly the same effect. You could even argue that polymorphism isn't much more than a convenient syntax for generating jump tables. (Robert Martin once elaborated on this in a nice newsgroup posting.) Of course it's making it *so* convenient that it actually very much changes the whole way you are programming once you groked it!
Inheritance and implementation of interfaces are two good ways to create polymorphic objects.
Actually, I don't really like how inheritance and polymorphism get intermixed in statically typed languages - at least not when inheritance is the *only* way to get polymorphism.
Joined: Jan 29, 2003
Back when VB had "objects" but no inheritance (1995 VB4? 5?) we did what I called "polymorphism by coincidence". We'd iterate through a collection of objects and try various methods on them. If a method worked, great! If not, we ignored the error and went on. Ah, the bad old days.