Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 7

Learn about Java's many shapes and find out how to accommodate generalities in your class hierarchies

  • Print
  • Feedback
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.

  • Print
  • Feedback

Resources