Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polymorphism? What's that? 4

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
What does polymorphism mean in relation to java? I can't remember! This is what said:

polymorphism
A concept first identified by Christopher Strachey (1967) and
developed by Hindley and Milner, allowing types such as list
of anything. E.g. in Haskell:
length :: [a] -> Int
is a function which operates on a list of objects of any type,
a (a is a type variable). This is known as parametric polymorphism. Polymorphic typing allows strong type checking
as well as generic functions. ML in 1976 was the first language with polymorphic typing.
Ad-hoc polymorphism (better described as overloading) is the
ability to use the same syntax for objects of different types, e.g. "+" for addition of reals and integers or "-" for unary
negation or diadic subtraction. Parametric polymorphism
allows the same object code for a function to handle arguments
of many types but overloading only reuses syntax and requires
different code to handle different types.

I can't understand that, it doesn't seem to make any sence to me or anyone else I've asked. Please help me!
 
Polymorphism goes hand-in-hand with Dynamic Method Binding and is one of the three tenets of Object-Oriented Programming.

Dynamic Method Binding doesn't commit you to an implementation until runtime. Basically you can write programs that expect a certain type(Interface) and the correct method will be used depending on the actual implementation of an object. Polymorphism allows you to substitute objects that have identical interfaces.

A good example of both can be found in the Java Language itself.
Dynamic Method Binding
Code:
Object o = new String("test");
out.System.println(o.toString());
In this example o is a reference of type
Code:
Object
but the actual impletation is of type
Code:
String
. When
Code:
toString()
is called the JVM then determines that the proper
Code:
toString()
method to invoke is found in the
Code:
String
class not the
Code:
Object
class even though the reference is of type
Code:
Object
.

Polymorphism
Code:
A good example of polymorphism is found in the Java Collections API.  A [code]Collection
can take an
Code:
Object
as an element. This allows the developer to place any instantiated Java object into a
Code:
Collection
.

Hopes this helps you sort things out. One day it will just hit you and then you will understand. Wushutwist
 
I think I'm starting to understand. I hope I am anyway.
 
May I offer another example? You recall that an "Interface" in Java is similar to an abstract class. It has no implemetaion, and cannot be insantiated, because it is designed as a template for developers to follow to create a class that implements the functionality of the Interface.

Suppose you and I each build an object that implements a particular Java Interface. We each write our own code, so my "implementation" is different from your "implementation" . But the same Interface specification has been implemented by both of us.

This means that we have both written code to make all of the methods of the Interface work in our object, and we each have included all of the fields(variables) found in the Interface specification. But how your code works is different from how my code works.

The beauty of polymorphism is that anybody on earth can call the same methods in both of our applications, and pass in the same parameters (if any) and they will get identical results. That's because we have met the criteria called for in the Interface specification. That's why it's called an Interface. Programs written by different developers can "interface" (interact with) with my program, your program, or any other program that implements the same Interface.

As a side note, each of us might have added extra functionality (bells and whistles) to our objects, further distinguishing them from each other. But the basic functionality of the Interface is still there.

Just for fun you might want to look up the biological definition of polymorphism, which has something to do with the same organism taking on different forms.

Hope this helps,

Greg

 
Maybe these additional comments will clarify the subject even further:

- Strachey has defined the mentioned two types of polymorphism, but after him many more came with additional or different classifications, so just using the terms 'ad-hoc polymorphism' and 'parametric polymorphism' can be confusing, you always have to be detailed about what you are pointing at.

- when using the mentioned Strachey definitions and mapping them on JAVA:

* ad-hoc polymorphism: in JAVA a function-operator can be overloaded and overloading is a form of ad-hoc polymorphism (Sometimes ad-hoc polymorphism is called 'overloading', but other definitions subdivide ad-hoc polymorphism in different types including 'overloading'). For instance:

public void draw(Triangle atriangle) {...}
public void draw(Square asquare) {...}

The implementations can be different, as mentioned in the Strachey definition. Only the syntax is reused.

* parametric polymorphism: in JAVA an argument of a function can also be an object of a subclass of the class mentioned in the functiondefinition. For instance:

public void draw (Object anobject) {}
and at runtime we can get: <object>.draw(atriangle)

The implementation will be the same for the different types and the types have a common structure (all subclasses of the same superclass), which are two aspects always mentioned in the definition of parametric polymorphism.


- Sometimes parametric polymorphism is described as the ability to redefine methods for derived classes, such as in < This definition doesn't match Stracheys definition of parametric polymorphism, but is a kind of polymorphism that holds for JAVA: when applying a function to an object of a subclass which redefines that function, the redefined function will be applied.

- &quot;Polymorphic typing allows strong type checking&quot; (sentence from Strachey's definition):
First, what is &quot;strong type checking&quot;? A language is strongly typed when all expressions are type consistent. When a language is strongly typed, its compiler can guarantee that the program will execute without type errors.

How to relate this to polymorphic typing? When using polymorphic typing, one only knows at runtime the exact type of the argument, but one can check at compiletime if the argument belongs to the group of types accepted by a function. For instance:

public Object giveShape() {}
public void draw (Object object) {}

<object>.draw (<object>.giveShape())

This will compile correctly, but the handled object can
be a triangle, a rectangle or something different. This
will only be known at runtime.

But, because of the possibility of classcasting, JAVA isn't strongly typed and the compiler can't guarantee an execution without type errors. A ClassCastException can indeed occur.

Hope this gave you a better insight.

Gert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top