The words override and overload write and sound differently from each other and have difference in meaning. Overriding a method is an entirely different thing from overloading a method.
The overloading methods and overloading operators is not the end of the story. If a derived class customises the behaviour of methods defined in the base class, usually to meet the special requirements of the derived class, we say about runtime polymorphism.
JAVA supports the notion of overriding a method in a base class to cause it to behave differently relative to objects of the derived class. In other words, a method named drinking() that is defined in the base class and is overridden in the derived class would behave differently depending on whether it is invoked by an object of the base class or by an object of the derived class.
In JAVA reference variable of a base-class type can be used to reference an object of any class derived from that base class.
If an overridden method is invoked using that reference variable, the system will be able to determine, at runtime, which version of the method to use based on the true type of the object, and not on the type reference variable used to invoke the method. This fact is illustrated in the following JAVA program.
The first, an overridden method named drinking() as a base-class reference to a base-class object is invoked, then, as the second, the overridden method named drinking() is invoked on a derived-class reference to a derived-class object.
In the first case, the base-class version of drinking() is actually invoked. In the second case, the derived-class version of drinking() is invoked. This is trivial.
Then as the third, is invoked the overridden method named drinking() on a base-class reference which refers to a derived-class object. When this is done, it is the version of the method defined in the derived class and not the version defined in the base class that is actually invoked. This is the essence of runtime polymorphism.
1 // This program illustrates the run-time polymorphism.
2 // Output of the program should be as followed:
3 // My name is null and I am null and I am ready to work.
4 // I like drink with friends
5 // My name is Olexiy and I am Ukrainian and I am ready to work.
6 // and I am a Gentleman.
7 // I like drinking beer in my favorite pub
8 // My name is Olexiy and I am Ukrainian and I am ready to work.
9 // and I am a Gentleman.
10 // I like drinking beer in my favorite pub
11
12 class ex4 {
13 public static void main(String args[]) {
14 // Human class ref to Human class object
15 Human obj = new Human();
16 // Gentleman class ref to Gentleman class object
17 Gentleman me = new Gentleman();
18 // set information about me:
19 me.setPerson (0, "Olexiy", "Ukrainian");
20
21 // Display informatio about obj:
22 System.out.println( obj.getHumanInfo() );
23 // invoke method named drinking:
24 obj.drinking();
25
26 // Display information about me:
27 System.out.println ( me.getHumanInfo() );
28 // invoke method named drinking:
29 me.drinking();
30
31 // Human class ref to Gentleman class object
32 obj = me;
33
34 // Display information about obj:
35 System.out.println (obj.getHumanInfo() );
36 // invoke method named drinking:
37 obj.drinking();
38 }
39 }
Inheritance and method overriding are used in almost all JAVA programming. Even the well-known ``Hello World'' JAVA applet requires that the Applet class be extended and the paint() method be overridden.