Home arrow Java & J2EE arrow Page 6 - The Genius of Java

Streamlined Support for Polymorphism - Java

In this first chapter from the book The Art of Java by Herbert Schildt and James Holmes, the authors highlight certain features of the Java programming language that separate it from other languages. The chapter also covers: memory management, Java's built-in support for multithreading, Java's approach to exceptions as compared to C++, Java's support of polymorphism, and how bytecode enables Java's "Write Once, Run Anywhere" ability and provides security. (ISBN 0-07-222971-3, McGraw-Hill/Osborne, 2003).

  1. The Genius of Java
  2. Simple Types and Objects - The Right Balance
  3. Memory Management Through Garbage Collection
  4. A Wonderfully Simple Multithreading Model
  5. Fully Integrated Exceptions
  6. Streamlined Support for Polymorphism
  7. Portability and Security Through Bytecode
  8. The Richness of the Java API
  9. The Applet, and The Continuing Revolution
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 69
May 05, 2004

print this article



Polymorphism is the attribute of object-oriented programming that allows one interface to be used by multiple methods. Java supports polymorphism with a variety of features, but two stand out. The first is the fact that every method (other than one marked final) can be overridden by a subclass. The second is the interface keyword. Let’s examine each a bit closer.

Because methods in a superclass can be overridden by those in a derived class, it’s trivially easy to create class hierarchies in which subclasses are specializations of the superclass. Recall that a superclass reference can be used to refer to any subclass of that superclass. Furthermore, a call to a method on a subclass object, through a superclass reference, automatically executes the overridden version of that method. Thus, a superclass can define the form of an object and provide a default implementation. This default implementation can then be customized by a subclass to better meet the needs of a specific situation. Thus, the same interface, in this case the one defined by the superclass, can be the basis for multiple implementations.

Of course, Java takes the concept of “one interface, multiple methods” a step further. It defines the interface keyword, which allows you to fully separate a class’ methods from their implementations. Although an interface is abstract, you can still declare a reference of an interface type. This reference can then be used to refer to any object that implements the interface. This is a very powerful concept because it streamlines and facilitates the use of polymorphism. As long as a class implements an interface, an object of that class can be used by any code that requires the functionality provided by the interface. For example, assuming an interface called MyIF, consider the following method:

<FONT face="courier new, courier, mono">void myMeth(MyIF ob) {
// ...

Any object that implements the MyIF interface can be passed to myMeth( ). It doesn’t matter what other capabilities (if any) that object has. If it implements MyIF, then myMeth( ) can operate on it.

Remember: this is chapter one of The Art of Java, by Herbert Schildt and James Holmes (McGraw-Hill/Osborne, ISBN 0-07-222971-3, 2003). Check it out at your favorite bookstore today.  
Buy this book now.

>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: