Polymorphism


Polymorphism is described as "One interface, many implementations ." It is a way of imposing a generic structure onto a group of related code elements. Java implements polymorphism by defining code structures that serve as blueprints for a wide range of classes and interfaces while giving the classes and interfaces the freedom to implement the blueprint as they see fit.

An abstract class is an example of polymorphism. An abstract class can define abstract methods by declaring a method's signature, including the return type, method name , and parameter list. The abstract methods provide no implementation. A subclass of the abstract class then implements the abstract class methods providing whatever functionality is required by the subclass. Different subclasses can implement the methods in different ways as long as the signature and accessibility are the same.

Interfaces are another example of polymorphism. They are similar to abstract classes in that they define method signatures without giving any implementation for the methods. It is up to a class that implements the interface to provide the method implementations, and the class has complete freedom in how it does this.

Polymorphism can also be seen in the fact that Java allows method overloading, letting you create different versions of a method. Each version will have the same function but will take different input arguments. An example of an overloaded method is the abs() method from the java.lang.Math class. There are four versions of the abs() method, each taking a different input argument type, but the four methods share a common name and function.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net