Method overloading and overriding in Java with example

Method overloading and overriding in Java

In this post we will discuss about method overloading and overriding in Java. These two are important concepts and the difference between them is one of the frequently asked Java interview questions. Let us explore them with example.

Method overloading

Method overloading is nothing but having two or more methods in the same class with the same name but different number/type of arguments. Method overloading is resolved using static binding at compile time. You can also overload a constructor in Java. To overload a method, you must either need to change number of argument, type of argument or order of argument if they are of different types. Since return type is not part of method signature simply changing return type will not be considered as method overloading. Static methods can also be overloaded in Java. Method overloading is an example of compile time polymorphism (static polymorphism).

Overloading Example

Above is a simple program which shows overloading of add() method. The method implementation to bind to the method call is resolved at the compile time itself.

Method Overriding

Method overriding is nothing but having two methods with the same name, same arguments, but different implementations. Method overriding can be done in sub class only. So, to override a method, you need to create a child class which extends parent class and create a method in child class with same name and same arguments. The return type can be same type as that of method in parent class or it can be a sub type of it. The method implementation to bind to the method call is resolved at run-time based on object using dynamic binding in Java. Method overriding is an example of run-time polymorphism (dynamic polymorphism).

Overriding example

Had the run-time polymorphism not kicked in, the output would have been: “Driving vehicle …” for all the three invocations of drive() method. You can also see that truckVehicle.load() results in a compile time error. So what’s happening in the above code?

Any object declaration and instantiation has 2 parts in it: The type of the reference and the type of the object created. For example in Vehicle carVehicle = new Car() the reference type is Vehicle and the object created is of type Car. Such an assignment is only possible when the object created type is a subclass of the reference type i.e in cases where inheritance is used.

Each object reference can be used to invoke methods and the methods which can be invoked is decided based on the reference type. And this is decided during the compile time. But the implementation to be invoked is decided based on the type of the object created. In the above example: carVehicle.drive() compiles because the drive() method is part of the Vehicle class and gives “Driving car…” as the output because the method is overridden by the Car class. On similar lines: truckVehicle.load() gives compile time error because the method load() is not part of the Vehicle class, but is defined only in the Truck class. But the truck.load() compiles because the reference type is Truck class and the compiler can resolve the load() method.

Difference between Method overloading and overriding in Java

Method overloadingMethod overriding
Method overloading is nothing but having two or more methods in the same class with the same name but different number/type of arguments.Method overriding means having two methods with the same name, same arguments, but different implementations.
Overloading happens at compile-time so the binding of overloaded method call to its definition happens at compile-time.  This is called static bindingBinding of overridden method call to its definition happens at runtime. This is called dynamic binding
Static methods can be overloaded which means a class can have more than one static method of same name.Static methods cannot be overridden, it can be hidden.
Overloading is being done in the same classOverriding happens between a parent and a child class
This is also called as compile time polymorphismThis is also called as run time polymorphism
private and final methods can be overloaded.A child class cannot override the private/final methods of its base class.
Return type of method does not matter in case of method overloading. It can be same or different.Return type should be the same or subtype as that of the return type of overridden method.
Overloaded method can declare any exception in throws clause as per your needWhile overriding a method it can only throw exception declared by the overridden method or any subclass of it, means if overridden method throws IOExcpetion then overriding method can throw sub classes of IOExcpetion e.g. FileNotFoundException but not wider exception e.g. Exception.

 

Hope you find this post useful. If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
One comment

Add Comment

Required fields are marked *. Your email address will not be published.