Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jaxson Brown   on Aug 12 In Java Category.

  
Question Answered By: Fred Hicks   on Aug 12

Polymorphism has nothing to do with multiple inheritance.

In C++ it is the "virtual" keyword that gives you polymorphism. E.g. an array
of shapes can contain instances of circle, square, triangle, etc, child classes.
(I.e. these classes will all inherit from "shape" as a parent.)

Calling the "draw" method for the items in the array (i.e. at the "shape" level)
will cause the individual "draw" methods for the various children to be used -
provided they are all declared as "virtual".

In C++, methods are not virtual by default, so you need to declare them as such
if you want polymorphic behaviour. In Java, there is no "virtual" keyword, and
polymorphism is the default.

The question of whether Java's interface facility qualifies as polymorphic is a
more vexed one. Inasmuch as it is simply part of the definition of a child
class, it is an integral part of Java's inbuilt polymorphism. When you consider
the ability to attach to different interfaces, that is probably not actually
polymorphic, but it's pretty close.

In fact, my C++ opinion is that Java's interface facility is simply its weird
way of implementing multiple inheritance - once they found that you really
couldn't survive without it.

Share: 

 

This Question has 23 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Polymorphism Or get search suggestion and latest updates.

Related Topics:

Tagged: