• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Advantages of oops

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is uses or advantages of Inheritances and polymorphism in oops.
 
Ranch Hand
Posts: 3404
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many great people say no to inhertinace and say yes to delgation.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic