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 - Overriding Methods

  • fields cannot be overridden but they can be hidden ie if you declare a field in a subclass with the same name as one in the superclass, the superclass field can only be accessed using super or the superclasses type
  • a subclass can override methods in it's superclass and change it's implementation
  • it must have the same return type, name, and parameter list and can only throw exceptions of the same class/subclass as those declared in the original method
class Super {
    void test() {
        System.out.println("In Super.test()");
    }
}

class Sub extends Super {
    void test() {           // overrides test() in Super
        System.out.println("In Sub.test()");
    }
}
  • cannot have weaker access rights than the original method
In Sub class:
    // compile-error, original has package access
    private void test() {}      
    protected void test() {}    // compiles ok
    public void test() {}       // compiles ok    
  • you can have multiple overloaded methods in a class but only one overriding method
In Sub class:
  void test() {}          // overrides test() in Super
  public void test() {}  // compile-error: test() already declared
                         // different access modifiers not part of
                         // method signature for naming purposes
  void test(String str) {}// compiles ok, overloads test()
  • Only accessible non-static methods can be overridden
  • static methods can be hidden ie you can declare a static method in the subclass with the same signature as a static method in the superclass. The superclass method will not be accessible from a subclass reference
  • any class can override methods from its superclass to declare them abstract, turning a concrete method into an abstract one at that point in the type tree. Useful when a class's default implementation is invalid for part of the class hierarchy (JPL pg 77)

Overriding with constructors

  • you cannot override a constructor in a superclass as they are not inherited
  • you cannot override a constructor in the same class as they would both have the same signatures; get an 'already declared' compile-error
  • if you're instantiating a Subclass object and if the Superclass constructor calls a method that is overridden in the Subclass, the Subclass method will called from the superclass constructor -- NOT the one in the superclass
class Super {
    Super(){
        System.out.println("In Super constructor");
        test();
    }
    
    void test() {
        System.out.println("In Super.test()");
    }
}

class Sub extends Super {
    Sub() {
        System.out.println("In Sub constructor");
    }
    
    void test() {       // overrides test() in Super
        System.out.println("In Sub.test()");
    }
}

Output if Sub sb = new Sub() is invoked:
    In Super Constructor
    In Sub.test()
    In Sub Constructor

Also see

Example Code

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