• 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

Polymorphism Vs Inheritence

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody!
Can polymorphism exist without inheritence?
Thanx for any reply
Preethi
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, yes, polymorphism can exist without inheritance, through the use of interfaces:

[This message has been edited by Frank Carver (edited December 21, 2000).]
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some might say that interfaces are just an implementation detail and that the above code is a good example of inheritance.
I mean, when you look at polymorphism without reffering to any particular language, it is based on inheritance. Just because java calls a certain type of class an interface, does this mean there is no inheritance?
Something for the java philosophers, perhaps.
Grant.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether we're calling it Java philosophy or just good 'ol language lawyering, I'd disagree with Grant's summation. But it may just be a difference in semantics.
A class can implement of an interface, but not the reverse, of course. Interfaces "do" nothing. What they provide is a static reference to a description of behavior that can be checked at compile-time. If several classes implement the same interface, then we can say they share a "facet" or "aspect" of behavior, or that they conform to the same type description.
With inheritance from a common class, we have an expectation of common code, or behavior, from which each subclass varies. If Button is the parent class, for example, we might then have RectangularButton, ChamferedButton, OvalButton as subclasses. We assume variation on a theme is what's going on. Tthe concept of inheritance dominates, but we have method overriding and overloading to help us achieve polymorphic effect.
Now consider an interface called Widget, which declares the method activate(). In practice, implementations of Widget really share nothing more than the idea of activate. Any class that implements Widget and therefore has something to activate() qualifies as a Widget type. But there's no assurance, beyond "properly" implementing the method itself, that any two implementations of Widget will look anything like each other. Polymorphism expresses the greatest possible flexibility with interfaces, and in polymorphism, flexibility is synonymous with power.
Having said that, I don't see what the question is driving at. I can't say I would *want* polymorphism without inheritance...
-----------------
Michael Ernest, co-author of:
The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited December 21, 2000).]
 
Preethi Suryam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank and all!
Thanx a lot for enlightening me with some extra knowledge.
Thanx once again!
preethi
reply
    Bookmark Topic Watch Topic
  • New Topic