On the previous articles we mentioned both overloading and overriding but we didn’t pay sufficient attention to them. Those two meanings are among the most important ones in Object Oriented programming and design. It’s not a matter of usage it’s a matter of design and elegant programming. Let’s take a closer look on both of them.
Overloading
As seen before a method’s signature (if you prefere uniqueness) depends uppon these things:
- Method name
- Method parameters
So, you cannot define two methods that have all the above the same in one class, but you can define two seperate methods with the same name that have different method parameters. Here is a quick example.
Test.java
public class Test{
public void printMsg(String msg){
System.out.println(msg);
}
public void printMsg(String msg, int num){
System.out.println(msg + " " + num);
}
}
As you can see, printMsg has different parameters. Now one would argue, what would be different if we created one method called printMsgString and printMsgNum with the appropriate parameters? Well, as a matter of fact, none. The program would have the exact same functionality. But, one major difference. It’s not elegant. The code is better readable when creating the method with the same name (since the same thing happens on both methods). On the model this is the right practice. The class has two overloaded methods that do printing. How the printing will be done exactly depends upon the method parameters that we will pass through. If we pass a String then the first one will be called, if we pass a String and a number the second one will be fired.
All in all, overloading, is a method that is used to give the class a way to say what it can do in which ways (for the above example the class Test can print either giving a single string or a string and a number).
Overriding
This is a somewhat same but also different thing. Overriding comes between methods, not of the same class as overloading does, but of classes that subclass others. Let’s take the example below:
Mother.java
public class Mother{
public void printMsg(){
System.out.println("Mother method!");
}
}
Child.java
public class Child extends Mother{
public static void main(String args[]){
(new Child()).printMsg();
}
public void printMsg(){
System.out.println("Child method!");
}
}
The Child class subclasses the Mother class. It also has a main method to start. A small pointer here. Notice that in one line in the main method we instantiate the Child and on the instance created we call the printMsg method. For something so simple there is no need to keep the instance on a variable.
Now the output of the above example, upon running the Child class, there will be “Child method!” printed on the console. Now, what if, for some reason, we wanted the mother’s method to be executed at some point within the child’s method. There comes the keyword super. This, along with the keyword “this” are automaticaly defined when instantiating. The keyword “this” holds the reference to the current instance and super hold the reference to the superclass. So if we wanted to call the mother’s method we would use this slightly different Child class:
Child.java
public class Child extends Mother{
public static void main(String args[]){
(new Child()).printMsg();
}
public void printMsg(){
super.printMsg();
System.out.println("Child method!");
}
}
The output of the above example would be “Mother method!” an empty line and then “Child method!” printed out on the console.
This should make things clear both for overriding and overloading. Before going on you must get the grasp of these because they are used widely on all the classes of the jdk.
For any problem, question or anything at all, as always, leave a comment!
Nice tutorial. Explained with suitable examples. I love java programming…
@Arul: thx man… hope you like it and come back again 😉
Hi,
Very clear about overwriting and overloading. Need it for an examen concerning OO. Thanks a lot.
Dennis