Object-oriented language basics, Part 7
Learn about Java's many shapes and find out how to accommodate generalities in your class hierarchies
By Jeff Friesen, JavaWorld.com, 10/05/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Polymorphism, a fundamental object-oriented programming principle, tends to confuse novice Java developers. Part of the bewilderment
stems from the fact that, although there are four types of polymorphism, Java officially supports only three of them (at the
moment). This article attempts to clear away the clouds surrounding polymorphism by exploring each polymorphism type -- with
emphasis on the inclusion polymorphism category.
After discussing polymorphism, I turn to abstract classes and abstract methods. Abstract classes prevent developers from creating
objects out of those classes that represent generic concepts. In addition, abstract classes often work with inclusion polymorphism.
Though abstract classes and interfaces do resemble one another, they also differ, and I will explain how in this article.
If you follow the Java 101 column, you know that we are currently touring Java's object-oriented language basics. So far, this series has covered class
declaration, object creation, field/method declaration and access, object composition, object layering, multiple implementation
inheritance, the root of all classes, and interfaces (with multiple interface inheritance). In addition, I have introduced
the specialized topics of enumerated types and singletons. Read the whole series on object-oriented language basics, and learn
what it means for a Java program to be object oriented:
Polymorphism
The quality or state of assuming different forms, or shapes, is known as polymorphism. Water provides a good real-world example of polymorphism. When frozen, water turns into ice. When that ice melts, a liquid
appears. Boil that liquid and you end up with a gas. Computer languages also manifest polymorphism in different ways. As Wm.
Paul Rogers points out in his excellent article "Reveal the Magic Behind Subtype Polymorphism," there are four kinds of polymorphism at the computer-language level: coercion, overloading, parametric, and inclusion.
Coercion polymorphism refers to a single operation serving several types through implicit type conversion. For example, the subtraction
operation manifests itself in source code through the subtraction operator (-). That operator allows you to subtract an integer from another integer and a floating-point value from another floating-point
value. However, if one operand is an integer and the other operand a floating-point value, the compiler must convert the integer's
operand type to floating-point. Otherwise, a type error occurs -- because Java's subtraction operation does not subtract integers
from floating-point values, or vice versa. Another example of coercion polymorphism involves method calls. If a subclass declares
a method with a superclass parameter, and if a call is made to that method with a subclass object reference, the compiler
implicitly converts the subclass reference type to the superclass reference type. That way, only superclass-defined operations
are legal (without explicit type casts) in the method.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download this article's source code and resource files
http://www.javaworld.com/javaworld/jw-10-2001/java101/jw-1005-java101.zip
- For a glossary specific to this article, homework, answers to Part 6's homework, and more, see the Java 101 study guide that accompanies this article
http://www.javaworld.com/javaworld/jw-10-2001/jw-1005-java101guide.html
- Ready for the next Java 101 lesson? Read "Initialize Classes and Objects," Jeff Friesen (JavaWorld, November 2001)
http://www.javaworld.com/javaworld/jw-11-2001/jw-1102-java101.html
- The Java Language Specification, Second Edition, James Gosling, Bill Joy, Guy Steele, Gilad Bracha (Sun Microsystems, 2000)
http://www.javasoft.com/docs/books/jls/second_edition/html/j.title.doc.html
- The Java Tutorial
http://www.javasoft.com/tutorial/
- The Javadoc Tips FAQ
http://java.sun.com/j2se/javadoc/faq.html
- Javadoc 1.3 documentation
http://java.sun.com/j2se/1.3/docs/tooldocs/javadoc/index.html
- Sun's official Java language definition
http://java.sun.com/docs/overviews/java/java-overview-1.html
- Learn what to do when
switch statements start reappearing in your code in "Java Tip 30Polymorphism and Java," Philip Bishop (JavaWorld): http://www.javaworld.com/javaworld/javatips/jw-javatip30.html
- Polymorphic behavior is best understood in terms of type, rather than as dependent on overriding implementation inheritance.
For more information, read "Reveal the Magic Behind Subtype Polymorphism," Wm. Paul Rogers (JavaWorld, April 2001)
http://www.javaworld.com/javaworld/jw-04-2001/jw-0413-polymorph.html
- Learn about the advantages of adding parametric polymorphism to Java in "Behold the Power of Parametric Polymorphism," Eric
Allen (JavaWorld, February 2000)
http://www.javaworld.com/jw-02-2000/jw-02-jsr.html
- When does it make sense to choose an abstract class over an interface? This article offers additional insight"Abstract Classes
vs. Interfaces," Tony Sintes (JavaWorld, April 2001)
http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html
- "Maximize Flexibility with Interfaces and Abstract Classes," Wm. Paul Rogers (JavaWorld, September 2001)
http://www.javaworld.com/javaworld/jw-09-2001/jw-0921-interface.html
- Go on to Jeff's next Java 101 lesson"Class and Object Initialization" (JavaWorld, November 2001)
http://www.javaworld.com/javaworld/jw-11-2001/jw-1102-java101.html
- Check out past Java 101 articles
http://www.javaworld.com/javaworld/topicalindex/jw-ti-jav101.html
- For more articles on Object-Oriented Design and Programming, browse our Topical Index
http://www.javaworld.com/channel_content/jw-oop-index.shtml
- Java experts answer your toughest Java questions in JavaWorld's Java Q&A column
http://www.javaworld.com/javaworld/javaqa/javaqa-index.html
- For Tips 'N Tricks, see
http://www.javaworld.com/javaworld/javatips/jw-javatips.index.html
- Sign up for JavaWorld's free weekly email newsletters
http://www.idg.net/jw-subscribe
- You'll find a wealth of IT-related articles from our sister publications at IDG.net