OVERRIDING

When you extend a class to produce a new one, you inherit and have access to all the non-private methods of the original class. When you want to redefine the behavior of one of these methods to suit your new class, you override the method to define a new one.
An overriding method replaces the method it overrides. Each method in a parent class can be overridden at most once in any subclass. Overriding methods should have argument lists of identical type and order. The return type of an overriding method must be identical to that of the method it overrides.
Some other special rules apply to overriding methods:
• The accessibility must not be more restricted than the original method
• The method must not throw checked exceptions of classes that are not possible for the original method.
Example #1:
class Animal {
void saySomething() {
System.out.println("Our kind is unable to speak");
}
}
class Dog extends Animal {
void saySomething() {
System.out.println("Wof!");
}
}
Animal a[] = new Animal[2];
a[0] = new Animal();
a[1] = new Dog();
a[0].saySomething();
a[1].saySomething();
Result:
Our kind is unable to speak
Wof!
Invoking Overridden Methods
It is very useful to be able to invoke an overridden method from the method that overrides it. The keyword “super” allows to access features of a class "up" in the hierarchy.
Example:
class Animal {
void saySomething() {
System.out.println("Our kind is unable to speak");
}
}
class Dog extends Animal {
void saySomething() {
super.saySomething();
System.out.println("Wof!");
}
}
Dog d = new Dog();
d.saySomething();
Result:
Our kind is unable to speak
Wof!

OVERLOADING

Overloading is about reusing the same method name with different arguments and sometimes different return type. Some basic rules apply:
• In the class that defines de original method, or a subclass of that class, the method name can be reused if the argument list differs in terms of the type of at least one argument.
• A difference in return type alone is insufficient to constitute an overload and is illegal.
Example #1:
public static void main(String args[]) {
System.out.println( DoubleIt("Hello world") );
System.out.println( DoubleIt(15) );
}
static String DoubleIt(String name) {
name += " " + name;
return name;
}
static String DoubleIt(int id) {
id *= 2;
return Integer.toString(id);
}
Result:
Hello Hello
30
Example #2:
int MyMethod(int a) { }
float MyMethod(int a) { } // ERROR!!!. Difference in return type alone
// is insufficient to constitute an overload.
Example #3:
int MyMethod(int value) { }
float MyMethod(int number) { } // ERROR!!!. The types should be
// different, not the variable names.
Invoking Overloaded Methods
Method overloaded names are effectively independent methods. Using the same name is just a convenience to the programmer. Overloaded methods may call one another simply by providing a normal method call with an appropriately formed argument list.
Example:
public static void main(String args[]) {
allStars(15);
}
static void allStars(String name) {
name = "*** " + name + " ***";
System.out.println(name);
}
static void allStars(int id) {
String s = ""+id;
allStars(s);
}
In this example, we have created a method to show names surrounded by stars, we also wanted to accept int values, so we added a method that receives ints but converts them to strings and passes the result to the original string method.