In an earlier post, we have seen different types of inheritance in Java as a part of our Java Programming tutorial.
In this post, we will see method overloading and method overriding in Java.
Method overloading and overriding are two different terminologies in programming. Let’s start with Java overloading, first.
What is Method Overloading?
Method Overloading in Java is an aspect of a class to include more than one method with the same name but vary in their parameter lists. It increases the readability of a program.
Why Method Overloading?
Suppose that we have a class “Main” to multiply numbers (two or more, float or int) and the class contains the new method for every kind of multiplication we perform. It is cumbersome to do this. So Java programming gives us a provision to perform the multiplication for all variations by using the same method name but just changing the arguments in the parameter list.
Different Ways to Overload a Method:
Java defines 2 varied ways to overload methods, and they are –
Java doesn’t support method overloading by changing the return type of the function only as it leads to ambiguity at compile time.
Let us have a look at the examples of the two cases that help us overload a method in Java.
Example 1: change the number of arguments
class Main { static int multiply(int a, int b) { int c = a*b; return c; } static int multiply(int a, int b, int c) { int z = a*b*c; return z; } public static void main(String args[]) { System.out.println(Main.multiply(12,12)); System.out.println(Main.multiply(6,6,6)); } }
Output:
144 216
Example 2: change type of arguments
class Main { static int multiply(int a, int b) { int c = a*b; return c; } static double multiply(double a, double b) { double z = a*b; return z; } public static void main(String args[]) { System.out.println(Main.multiply(12,12)); System.out.println(Main.multiply(12.5,11)); } }
Output:
144 137.5
Promoting a data type of small size to large size is known as type promotion. For instance, a byte can be promoted to short, int, long, float, or double. Similarly, char can be promoted to int, long, float, double and so forth.
Note that a data type can be promoted but not demoted to a lesser type implicitly.
A simple example of method overloading with type promotion is discussed below:
class Main { void product(int a,float b) { System.out.println(a*b); } void product (int a,int b,int c) { System.out.println(a*b*c); } public static void main(String args[]) { Main obj=new Main(); obj.product(12,12); obj.product(6,4,5); } }
Output:
144 120
Hitherto, we have seen method overloading in our tutorial about the Java program for method overloading and overriding.
Now let us learn in brief about method overriding.
What is Method Overriding in Java?
Using a method in the child class that already exists in the parent class is referred to as method overriding. In simple words, the child class is providing its own body to a method that is previously declared and defined by the parent class, i.e. the base class method is overridden by the derived class.
Method overriding is used for runtime polymorphism.
Rules for Method Overriding:
class Radio { void play() { System.out.println("Plays music"); } } class Caravan extends Radio { void play() { System.out.println("Plays old classical hits !!"); } } public static void main(String args[]) { Caravan obj = new Caravan(); obj.play(); }
Output:
Plays old classical hits !!
Finally,
In this tutorial, we have learned about two important concepts in Java, i.e., method overloading and method overriding.
If you ask me to simplify it, method overloading refers to using a method with the same name but a different list of parameters. And Method overriding refers to using of a parent class method in the child class with a varied method definition.
Any doubt about the above method overloading and method overriding in Java? Feel free to comment your question below.
Happy Learning!
« Types of Inheritance in JavaAbstraction in Java »