6.2. Overriding and Hiding Members

Instance Method Overriding

Under certain circumstances, a subclass may override non-static methods defined in the superclass that would otherwise be inherited. When the method is invoked on an object of the subclass, it is the new method implementation in the subclass that is executed. The overridden method in the superclass is not inherited by the subclass, and the new method in the subclass must uphold the following rules of method overriding:

  • The new method definition must have the same method signature (i.e., method name and parameters) and the same return type.

    Whether parameters in the overriding method should be final is at the discretion of the subclass (see Section 3.22, p. 94). A method's signature ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.