Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Overloading, Overriding, Runtime Types and Object Orientation - Polymorphism

  • polymorphism translates from Greek as many forms ( poly - many morph - forms)
  • in OOP's it refers to
    the propensity of objects to react differently to the same method (VA pg 110)
  • method overloading is the primary way polymorphism is implemented in Java

Overloading methods

  • overloaded methods:
    1. appear in the same class or a subclass
    2. have the same name but,
    3. have different parameter lists, and,
    4. can have different return types
  • an example of an overloaded method is print() in the java.io.PrintStream class
public void print(boolean b)
public void print(char c)
public void print(char[] s)    
public void print(float f)
public void print(double d)
public void print(int i)
public void print(long l)
public void print(Object obj)
public void print(String s)
  • the actual method called depends on the object being passed to the method
  • Java uses late-binding to support polymorphism; which means the decision as to which of the many methods should be used is deferred until runtime

Overriding methods

  • late-binding also supports overriding
  • overriding allows a subclass to re-define a method it inherits from it's superclass
  • overriding methods:
    1. appear in subclasses
    2. have the same name as a superclass method
    3. have the same parameter list as a superclass method
    4. have the same return type as as a superclass method
    5. the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
      • if the superclass method is public, the overriding method must be public
      • if the superclass method is protected, the overriding method may be protected or public
      • if the superclass method is package, the overriding method may be packagage, protected, or public
      • if the superclass methods is private, it is not inherited and overriding is not an issue
    6. the throws clause of the overriding method may only include exceptions that can be thrown by the superclass method, including it's subclasses
class LBException extends Exception {}
class LBException1 extends LBException {}

In superclass:
    public void testEx() throws LBException {        
        throw new LBException();        
    }    

In subclass:

    public void testEx() throws LBException1 {    
        throw new LBException1();        
    }    
  • overriding is allowed as LBException1 thrown in the subclass is itself a subclass of the exception LBException thrown in the superclass method

Side effect of late-binding

  • it is Java's use of late-binding which allows you to declare an object as one type at compile-time but executes based on the actual type at runtime
class LB_1 {
    
    public String retValue(String s) {
        return "In LB_1 with " + s;
    }
    
}

class LB_2 extends LB_1 {

    public String retValue(String s) {
        return "In LB_2 with " + s;
    }

}
  • if you create an LB_2 object and assign it to an LB_1 object reference, it will compile ok
  • at runtime, if you invoke the retValue(String s) method on the LB_1 reference, the LB_2 retValue(String s) method is used, not the LB_1 method
    LB_2 lb2 = new LB_2();
    LB_1 lb3 = lb2;         // compiles ok
    
    System.out.println(lb3.retValue("Today"));  
    
Output:
    In LB_2 with Today    

Example Code

Encapsulation Polymorphism isA/hasA Overloading Overriding Field Variables
Initialization Top-level Classes Inner Classes Static Nested Classes Local Classes Anonymous Classes