Java crash course #5: Overloading vs Overriding

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:

  1. Method name
  2. 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

  1. public class Test{
  2.     public void printMsg(String msg){
  3.         System.out.println(msg);
  4.     }
  5.     public void printMsg(String msg, int num){
  6.         System.out.println(msg + " " + num);
  7.     }
  8. }

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

  1. public class Mother{
  2.     public void printMsg(){
  3.         System.out.println("Mother method!");
  4.     }
  5. }

Child.java

  1. public class Child extends Mother{
  2.     public static void main(String args[]){
  3.         (new Child()).printMsg();
  4.     }
  5.     public void printMsg(){
  6.         System.out.println("Child method!");
  7.     }
  8. }

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

  1. public class Child extends Mother{
  2.     public static void main(String args[]){
  3.         (new Child()).printMsg();
  4.     }
  5.     public void printMsg(){
  6.         super.printMsg();
  7.         System.out.println("Child method!");
  8.     }
  9. }

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!



3 Responses to “ “Java crash course #5: Overloading vs Overriding”

  1. Arul says:

    Nice tutorial. Explained with suitable examples. I love java programming…

  2. stratosg says:

    @Arul: thx man… hope you like it and come back again ;)

  3. dennis says:

    Hi,

    Very clear about overwriting and overloading. Need it for an examen concerning OO. Thanks a lot.

    Dennis

Leave a Reply