Java
Polymorphism
Polymorphism is the end effect of the Java class hierarchy. A specific object can also be thought of as a member of a class of more general objects which, in turn, can be thought of as a member of a class of even more general objects, all the way up to the Object class. Not only does this allow for a more logical object implementation, it also allows the programmer to design a general class once and then use it as a blueprint to create more specific classes. There are two ways in which Java classes exhibit polymorphism: by extending other classes (see Class hierarchy) and through interfaces.
Extending Other Classes
A (sub)class that extends another (super)class already has all of the attributes, the methods and fields, of the superclass. The subclass can (and should) also include methods that aren’t found in the superclass, override methods from the superclass to fit the specific needs of the subclass, or initialize fields that cannot be set to specific values in the superclass. The subclass should also have a constructor of its own, which may call the superclass constructor (Java implicitly calls the superclass constructor if it is not explicitly called by the program to ensure proper initialization).
Casting objects: When an object is instantiated, its type is set to whatever type the constructor returns. Like primitive types, however, objects may sometimes need to be cast. The syntax for casting objects is the same as that for casting primitive types:
-
(className)variable;
-
className variable = (className)expression;
-
When necessary, Java will automatically make widening casts, where the destination class is further up the class hierarchy (i.e., closer to the Object class) than the original class.
-
Narrowing casts, where the destination class is deeper in the class hierarchy than the original class, must be made explicitly by the programmer. The compiler will throw a Type Mismatch exception if the cast is necessary but not made.
-
It is illegal to try to cast an object to a destination class deeper in the hierarchy than the class in which the object was instantiated.
Encapsulation: Although a subclass’s constructor can have no obvious relation to its superclass’s constructor (classes that extend java.long.Object are often like this), the real power of object-oriented programming comes from re-using as much of the superclass as possible and then tweaking the superclass for the specificities of the subclass. The subclass can call the methods of the superclass without being aware of its implementation details. This encapsulates the superclass, providing separate modules and a layer of protection by hiding code and private data from the user.
The super keyword is used to refer to the superclass. Therefore, super() refers to the constructor of the superclass, super.name refers to the field name in the superclass, and super.myMethod() refers to the method myMethod() in the superclass.
Example:
-
public class Polygon throws Exception {
int sides;
public Polygon(int n) {
sides = n;
if (n < 3) throw TooFewSidesException}
public int angleMeasure() {
return 180 * (sides - 2); }
}
public class Triangle extends Polygon {
final int SIDES = 3;
public Triangle() {
super(SIDES); }
}
Method overriding: To reflect the specific behavior of a subclass in reference to its superclass, Java supports method overriding. “Overriding” means defining a method in the subclass with the same name and signature as a method in the superclass. When a method is called on a subclassed object, Java executes either the method in the subclass or the superclass, depending on the current class of the object. If the subclass has no method with the same name and signature as the called method, Java executes the method in the superclass.
-
Abstract methods are methods with names and signatures, but without bodies. Any class that contains abstract methods must be declared abstract and cannot be instantiated. Abstract methods must be overridden by subclasses or supported by implemented interfaces.
Interfaces
To simulate the multiple inheritance features of languages such as C++, Java supports interfaces, which are a contract between the programmer and the Java interpreter. Interfaces tend to have names (such as Runnable, Throwable, and Observable) that describe a function a particular class can perform, without making any claims about the class’s fields and structure.
Implementing interfaces: An interface is a specification that any class implementing the interface is guaranteed to support. The keyword implements declares that a class supports a particular interface, and although a class can extend only one other class, it can implement an unlimited number of interfaces:
Example:
-
class Suburban extends Car implements FourWheelDrive, SixCylinder
-
Attempting to let a class implement an interface without writing methods (and method bodies) for all the methods declared in the interface will cause a compile-time error.
Writing interfaces: Interfaces are written just like classes, except with the class keyword replaced by interface.
-
public interface Runnable { ... }
-
public interface MouseMotionListener { ... }
The body of an interface can contain only abstract methods and/or constant fields (declared as final and static). Any class implementing the interface must either supply the implementation for the abstract methods or be labeled abstract itself.
Examples:
-
public interface Runnable {
public void run() {}
} -
public interface MouseMotionListener {
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}
Polymorphism

