FAQ

Java

JSP

Servlet


Advertisement



What are method overloading and method overriding?

Each method in a Java class is uniquely identified by its method signature. A method's signature is its name and the number and the type of its arguments. The method return type, method modifiers, and declared thrown exceptions have no effect on the method signature.

A class cannot contain two methods with the same "method signature."

What is method overloading?

With the Java language, you can overload methods. Overloading is the practice of supplying more than one definition for a given method name in the same class. The compiler will automatically select the most appropriate one based on the arguments with which it is called.

Several restrictions govern an acceptable set of overloaded methods:

  • Any two methods in a set of overloaded functions must have different argument lists (The name of the parameter is irrelevant. The type and order of the parameters is what's important).
  • A difference in return type only is not sufficient to constitute an overload and is illegal.

Method overloading is generally used where a class can perform the same operation on more than one type of data.

What is method overriding?

A sub class inherits methods from a super class. Sometimes, the sub class may want to provide a new version of methods defined in the super class. This is referred to as method overriding. Method overriding allows a sub class to provide its own implementation of a method already provided by one of its super classes.

A sub class method is provided with the same signature and return type as a method that is declared in its super class, the subclass is said to override that method. You can also override a method with the same signature that returns a subclass of the object returned by the original method. This facility (introduced in 5.0) is called covariant return type.

The subclass method does not inherit the functionality of its super class method unless it explicitly calls the super class method using the "super" keyword.

Several restrictions govern any particular method to override another correctly:

  • The return type and method signature must be identical to those of a method in the super class.
  • The accessibility must not be more restrictive than original method.
  • The overriding method must not throw checked exceptions of classes that are not possible for the original method.

Example

Consider an example from 8.4.10.2 Example: Overloading, Overriding, and Hiding. In the example:

class Point {
int x = 0, y = 0;
void move(int dx, int dy) { x += dx; y += dy; }
int color;
}
class RealPoint extends Point {
float x = 0.0f, y = 0.0f;
void move(int dx, int dy) { move((float)dx, (float)dy); }
void move(float dx, float dy) { x += dx; y += dy; }
}

the class RealPoint hides the declarations of the int instance variables x and y of class Point with its own float instance variables x and y, and overrides the method move of class Point with its own move method. It also overloads the name move with another method with a different signature.

In this example, the members of the class RealPoint include the instance variable color inherited from the class Point, the float instance variables x and y declared in RealPoint, and the two move methods declared in RealPoint.

Which of these overloaded move methods of class RealPoint will be chosen for any particular method invocation will be determined at compile time by the overloading resolution procedure.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |