Sunday, March 18, 2007

Overriding, overloading, and object orientation

Polymorphism means "any forms." In object-oriented programming, it refers to the capability of objects to react differently to the same method. Polymorphism can be implemented in the Java language in the form of multiple methods having the same name. Java code uses a late-binding technique to support polymorphism; the method to be invoked is decided at runtime.

Overloaded methods are methods that have the same name, but different argument lists. Overriding, on the other hand, occurs when a subclass method has the same name, same return type, and same argument list as the superclass method.

Overloading
As we mentioned, it is mandatory for overloaded methods to have the same names but different argument lists. The arguments may differ in type or number, or both. However, the return types of overloaded methods can be the same or different. They may have different access modifiers and may throw different checked exceptions.

Consider a print() method used to output data. The data might be int, boolean, or char. For each type of data, the print() method should be implemented differently. We can use overloaded methods as shown here:


void print(int i) {}
void print(boolean b) {}
void print(char c) {}

The method invoked depends on the type of argument passed. A subclass can overload the methods, which it inherits from its superclass. Constructors can be overloaded, which allows us to instantiate a class with different types of arguments. In the following example, the subclass constructor invokes the overloaded constructor of the superclass, which takes an integer argument:


class Base
{
Base() {}
Base(int a)
{
System.out.println(a);
} //Overloaded constructors
}

class Derived
{
Derived(int a, int b){ super(a); }
}

Overriding
A subclass can redefine a method that it inherits from its superclass. Now the method actually called depends on the type of the invoking object at runtime. The overriding method must have the same name, arguments, and return type as the overridden method.

The overriding method cannot be less public than the overridden method. The overriding method should not throw new or broader checked exceptions that are not declared by the original method. In the following example, the overridden version of the print() method is invoked because the invoking object is an instance of the derived class:


class Base
{
void print()
{
System.out.println("Base");
}
}
class Derived extends Base
{
void print()
{
System.out.println("Derived");
}
public static void main(String args[])
{
Base obj = new Derived();
obj.print(); // "Derived" is printed
}
}

To invoke the superclass version of an overridden method from the subclass, use super.methodName(). In the above example, the subclass can use the functionality of the superclass print() method by calling super.print().

Methods declared as final cannot be overridden by subclasses. Even though constructors can be overloaded, they cannot be overridden because they are not inherited.

No comments: