1.5 Overriding & OverloadingHomepage  « Java6 Certification « 1.5 Overriding & Overloading

In this lesson we cover correct use of overriding and overloading of methods as well as identifying legal return values for methods.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 1: Declarations, Initialization and Scoping

    • Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant returns), for the method.

Overriding Methodsgo to top of page Top

A method override must adhere to the contract it has with the superclass method. This contract being, a method override must have exacly the same arguments and return type (or co-variant thereof) as the superclass method with the same method name. This is different to an overloaded method as discussed below and in the Objects & Classes section, in the Methods lesson, in which the argument lists has to be different. In fact if you change the argument lists, that's what you end up with an overloaded method. So to make sure we are doing a method override we need to keep the contract.

Overriding Method Rules

When we override a superclass method there are rules to adhere to:

  • A method override must have exacly the same arguments as the superclass method with the same method name or you end up with an overloaded method.
  • A method override must have the same return type (or co-variant thereof) as the superclass method with the same method name.
  • When we override a method, the overriding method can't be less accessible, but can be more accessible.
  • You cannot override a method marked with the final modifier.
  • You cannot override a method marked with the private access modifier. Even if you have a method in the subclass with the same method name and arguments as a private method in the superclass it knows nothing of that method as it can't see it and is just a normal non-overriding method
  • You cannot override a method marked with the static modifier as overridden methods pertain to instance methods and not the class. In fact if you use the same static method and parameters in a subclass as a static method in a superclass this is known as method hiding and is discussed in Static Overrides?.
  • An overriding method can throw any Error or RuntimeException exceptions, whether these are declared in the overridden method or not.
  • An overriding method must not throw any new checked exceptions or any checked exceptions that are higher up the inheritance tree than those declared in the overridden method.
  • An overriding method can throw checked exceptions that are lower in the inheritance tree than those declared in the overridden method, or throw fewer or none of the checked exceptions that were declared in the overridden method.


/*
  Some code showing method override rules
*/
import java.io.*; // Import IO exception classes

public class A { 
    void methodA(int i) { }
    final void methodB() {}
    private void methodC() {}
    protected void methodD(String s) {}
    protected void methodE(String s) {}
    void methodF(String s, int i) {}
    int methodG(String s, int i) { return i; }
    int methodH(long l, int i) { return i; }
    public A methodI() { 
        A a = new A();
        return a;
    }
    void methodJ() { }
    void methodK() { }
    void methodL() throws IOException { }
    void methodM() throws IOException { }
    void methodN() throws IOException { }
}


import java.io.*;  // Import IO exception classes

public class B extends A { 
    void methodA(int i) { }                     // OK, valid override
    void methodA(String s, int i) { }           // OK, this is an overloaded method
    final void methodB() {}                     // No, you can't override a method marked as final  
    void methodC() {}                           // OK, knows nothing of private method in superclass
    void methodD(String s) {}                   // No, override can't be less accessible
    public void methodE(String s) {}            // OK, override can be more accessible
    protected void methodF(int i, String s) {}  // OK, this is an overloaded method
    void methodF(String s, int i) { }           // OK, valid override
    int methodG(String s, int i) { return s; }  // No, imcompatible return types
    int methodH(long l, int i) { return i; }    // OK, valid override
    public B methodI() {
        B b = new B();
        return b;
    }                                           // OK, covariant (subtype) returned
    void methodJ() throws RuntimeException { }  // OK, overriding method can throw any Error  
                                                // or RuntimeExceptions, whether these are 
                                                // declared in the overridden method or not.
    void methodK() throws IOException { }       // No, an overriding method must not throw 
                                                // any new checked exceptions than are not 
                                                // declared in the overridden method.
    void methodL() throws Exception { }         // No, overriding method must not throw checked 
                                                // exceptions higher up the inheritance tree 
                                                // than those declared in the overridden method.
    void methodM() throws EOFException { }      // OK, overriding method can throw checked 
                                                // exceptions lower in the inheritance tree 
                                                // than those declared in the overridden method.
    void methodN() { }                          // OK, overriding method can throw fewer  
                                                // or none of the checked exceptions
                                                // that were declared in the overridden method.
}

Overloading Methodsgo to top of page Top

You can use the same method name more than once in a class or subclass and this is known as overloading. When using overloading each method must have different parameter lists so the compiler knows which method to call. Having the same parameter types is fine as long as the order differs.

Overloading Method Rules

When we overload a superclass method or a method in the same class there are rules to adhere to:

  • A method overload must have a different argument list to any like named methods in the same class.
  • A method overload must have a different argument list to any like named methods in a superclass or you end up with a method override and all the rules that apply when using overrides as listed in the section above.
  • A method overload has no constraints on return types.
  • A method overload has no constraints on accessibility.
  • A method overload has no constraints on checked exceptions.


/*
  Some code showing method override rules
*/
public class A { 
    void methodA(int i) { }
    final void methodB() {}
    protected void methodC(String s) {}
    void methodD(String s, int i) {}
    String methodE(String s, int i) { return s; }
    void methodF() { }
}


import java.io.*;  // Import IO exception classes

public class B extends A { 
    void methodA(int i) { }                    // OK, same parameter list as superclass so an override
    void methodA(String s, int i) { }          // OK, this is an overloaded method of superclass
    final void methodB(int i) {}               // OK, parameter list differs from superclass method 
                                               // marked as final, so valid overload  
    void methodC(int i) {}                     // OK, no constraints on subclass overload accessibility
    protected void methodD(int i, String s) {} // OK, this is an overloaded subclass method
    void methodD(String s, int i) { }          // OK, valid override
    int methodE(int i, String s) { return i; } // OK, no constraints on subclass overload return types
    void methodF(int i) throws IOException { } // OK, no constraints on subclass overload
                                               // checked exceptions  
    void methodG() {}                          
    void methodG(String s) {}                  // OK, overloaded in same class
}

Related Java6 Tutorials

OO Concepts - Inheritance - Overriding Methods
OO Concepts - Inheritance Concepts - Accessing Superclass Overrides
Flow Control - Methods - Overridden Methods & Exceptions
Objects & Classes - Methods - Overloaded Methods
Objects & Classes - Methods - Overloaded varargs Ambiguities

go to home page Java6 Tutor Homepage go to home page Top

All the Java6 Certification Declarations, Initialization and Scoping lessons are listed below. Click a link to go to that lesson.

1: Declarations

1.1 Classes, Packages & Imports

1.2 Interfaces

1.3 Abstract Classes

1.4 Variables & Scope

1.5 Overriding & Overloading

Overriding Methods
Overloading Methods

1.6 Constructors / Nested Classes