Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Advantages of oops

 
Pree ty mark
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is uses or advantages of Inheritances and polymorphism in oops.
 
HS Thomas
Ranch Hand
Posts: 3404
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many great people say no to inhertinace and say yes to delgation.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic