"JavaRanch, where the deer and the Certified play" - David O'Meara
The really easy kind of polymorphism is called overloading in Java and other languages, and it means that in any class you can use the same name for several different (but hopefully related) methods.
The second, more complicated kind of polymorphism, true polymorphism, is resolved dynamically at runtime.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
People are often confused by other non-object-oriented features of Java, like method overloading, which are sometimes presented as object-oriented. Don't be fooled: If it isn't late binding, it isn't polymorphism.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Polymorphism is the capability to assume different forms. In object-oriented programming, this refers to the capability of objects to have many methods of the same name, but with different types of arguments.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Object-Oriented Programming Defined
Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.
Polymorphism
Another way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.
For example, if you define a method that takes a java.lang.Object as a parameter, it can accept any object in the entire Java platform. If you define a method that takes a java.awt.Component as a parameter, it can accept any component object. This form of cooperation is called polymorphism.
You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from java.lang.Object. It is repeated here to show you that Set collection can add a String object and an Integer object to the Set because the Set.add method is defined to accept any class instance that traces back to the java.lang.Object class.
Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message
"JavaRanch, where the deer and the Certified play" - David O'Meara
Because overloading has nothing to do with polymorphism!If there is no such thing as compile time polymorphism how can overloading occur?
Basically the same message? The messages are different therefore there is no polymorphism involved. Here are two methods:In overloading, the same object responds differently to what is basically the same message.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from java.lang.Object. It is repeated here to show you that Set collection can add a String object and an Integer object to the Set because the Set.add method is defined to accept any class instance that traces back to the java.lang.Object class.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Polymorphism
Another way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.
For example, if you define a method that takes a java.lang.Object as a parameter, it can accept any object in the entire Java platform. If you define a method that takes a java.awt.Component as a parameter, it can accept any component object. This form of cooperation is called polymorphism.
You saw an example of polymorphism in Part 2, Lesson 5: Collections where a collection object can contain any type of object as long as it descends from java.lang.Object. It is repeated here to show you that Set collection can add a String object and an Integer object to the Set because the Set.add method is defined to accept any class instance that traces back to the java.lang.Object class.
String custID = "munchkin";
Integer creditCard = new Integer(25);
Set s = new HashSet();
s.add(custID);
s.add(creditCard);
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
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.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
"JavaRanch, where the deer and the Certified play" - David O'Meara
Originally posted by Cindy Glass:
Oh and from a Sun white paper
From http://java.sun.com/docs/white/langenv/Object.doc1.html
3.1 Object Technology in Java
Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message
Note that is says different OBJECTS not different METHODS.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Originally posted by Cindy Glass:
And Thomas, if you have
myMethod(Object x){
}
And x can be any sub-class of Object - that is EXACTLY what polymorphism is all about.
Polymorphism--the same message sent to different objects results in behavior that's dependent on the nature of the object receiving the message
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
public void calcTotal(int i);
public void calcTotal(Vector v);
How can you say that a Vector and int are "bascially the same message"? In fact the methods may have nothing to do with each other and the name being the same is just an accident.
Jane, your example of bark does not really prove that overloading is polymorphism. The whole point of polymorphism is that the
caller does not know what's actually going to happen when a "polymorphic" method is called. It depends on the actual object.
In your example, the caller "knows" that bark(), bark(int loudness) are two diferent properties of Dog and is he/she knows that will happen.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Originally posted by Jane Griscti:
The intent of the message is the same; with minor modifications. You want the dog to bark. If overloading is not polymorphic you would have to write barkLoud, barkTwice, etc. With overloading you can summarize your intent with one 'verb' and then tack on 'adverbs'. Simplifies things.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Why? A method is defined by the combination of name and parameters so two methods with the same name are not the same if they have different paramters.If overloading is not polymorphic you would have to write barkLoud, barkTwice, etc
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
"JavaRanch, where the deer and the Certified play" - David O'Meara
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
My book, my movies, my videos, my podcasts, my events ... the big collection of paul wheaton stuff!
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Since this is the Sun certification forum, we go by what Sun puts on their site.Originally posted by Paul Wheaton:
We need to form legions of bigotry. We need to poo-poo people who beleive that overloading is polymorphism.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Originally posted by Cindy Glass:
All Polymorphism involves sub-classing or implementing interfaces and the features of these activities.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|