Tuesday, May 18, 2010

SCJP Exam Watch 6 - Object Orientation (2)

Be careful to recognize when a method is overloaded rather than overridden. You might see a method that appears to be violating a rule for overriding, but that is actually a legal overload, as follows:

public class Foo {
    public void doStuff(int y, String s) {}
    public void moreThings(int x) {}
}

class Bar extends Foo {
    public void doStuff(int y, long s) throws IOException {}
}

It's tempting to see the IOException as the problem, because the overridden doStuff() method doesn't declare an exception, and IOException is checked by the compiler.

But the doStuff() method is not overridden! Subclass Bar overloads the doStuff() method, by varying the argument list, so the IOException is fine.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

No comments:

Post a Comment

Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.