Java: overriding methods: what it means, and rules about it

Tutoring Java programming, you might be asked to clarify about overriding. The tutor shares some thoughts.

In Java, when you override a method, you either

  1. define its performance (give it a “body”), or else
  2. redefine (change) its performance
  3. .

Case 1, above, refers to the situation of writing a subclass of an abstract class, or implementing an interface: in either case, a method with undefined functionality must (typically) be defined, aka overridden.

Case 2 often refers to the situation of writing a subclass that alters the performance of an inherited method. However, an instance of a class can have a method overridden as well.

Rules:

When overriding a method, its return type, name, and parameter list (including types) must be identical to its original ones [from the abstract class, interface, or base class].

@Override can be used at the beginning of the line where the override is declared. For example:

@Override public void method_x(String param0, int param1,….){

This may give the compiler extra opportunity to check the validity of the override, as well as improve the code’s human readability.

Source:

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply