What is method overloading and method overriding explain with example?

Java 8Object Oriented ProgrammingProgramming

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.

Example of Method Overloading

If you observe the following example, here we have created a class called Sample and this class has two methods with the same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).

When you invoke the add() method based on the parameters you pass respective method body gets executed.

public class Sample{    public static void add(int a, int b){       System.out.println(a+b);    }    public static void add(int a, int b, int c){       System.out.println(a+b+c);    }    public static void main(String args[]){       Sample obj = new Sample();       obj.add(20, 40);       obj.add(40, 50, 60);    } }

On execution, it will produce the following output −

60 150

Method Overriding in Java

In method overriding, Super class and subclass have methods with the same name including parameters. JVM calls the respective method based on the object used to call the method. In overriding, the return types should also be same.

Example of Method Overriding

Let's take an example to understand how method overriding works in Java −

class SuperClass{    public static void sample(){       System.out.println("Method of the super class");    } } public class SubClass extends SuperClass {    public static void sample(){       System.out.println("Method of the sub class");    }    public static void main(String args[]){       SuperClass obj1 = new SubClass();       SubClass obj2 = new SubClass();       obj1.sample();       obj2.sample();    } }

Here we have a SuperClass and a SubClass. Both the classes have a method called Sample() with the same signature. In the main class, we created obj1 for SuperClass and obj2 for SubClass. The JVM calls the respective method based on the object used to call the method.

On execution, it will produce the following output −

Method of the super class Method of the sub class

Difference between Overloading and Overriding

The following table highlights the major differences between Method Overloading and Method Overriding −

Method OverloadingMethod Overriding
Method overloading is known as compile-time polymorphism.Method overriding is known as runtime polymorphism.
For overloading to come into picture, there must be at least two methods of the same name.For overriding to work, we need to have at least one method with the same name in both the parent class as well as the child class.
The methods must have different number of parameters. If both the methods have the same number of parameters, then their type must be different.Both the methods must have the same number of parameters with the same type.

Conclusion

Overloading and Overriding are concepts in object-oriented programming that are used to improve the readability and reusability of the programs. Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters.

Method Overriding is a mechanism to achieve polymorphism where the super class and the sub-class have same methods, including the parameters and signature. The JVM calls the respective method based on the object used to call the method.

What is method overloading and method overriding explain with example?

Updated on 03-Jun-2022 13:22:23

What is method overloading and method overriding explain with example?

Overriding and Overloading are two very important concepts in Java. They are confusing for Java novice programmers. This post illustrates their differences by using two simple examples.

1. Definitions

Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

2. Overriding vs. Overloading

Here are some important facts about Overriding and Overloading:

1). The real object type in the run-time, not the reference variable's type, determines which overridden method is used at runtime. In contrast, reference type determines which overloaded method will be used at compile time.2). Polymorphism applies to overriding, not to overloading.

3). Overriding is a run-time concept while overloading is a compile-time concept.

3. An Example of Overriding

Here is an example of overriding. After reading the code, guess the output.

class Dog{ public void bark(){ System.out.println("woof "); } } class Hound extends Dog{ public void sniff(){ System.out.println("sniff "); }   public void bark(){ System.out.println("bowl"); } }   public class OverridingTest{ public static void main(String [] args){ Dog dog = new Hound(); dog.bark(); } }

class Dog{ public void bark(){ System.out.println("woof "); } } class Hound extends Dog{ public void sniff(){ System.out.println("sniff "); } public void bark(){ System.out.println("bowl"); } } public class OverridingTest{ public static void main(String [] args){ Dog dog = new Hound(); dog.bark(); } }

Output:

bowl

In the example above, the dog variable is declared to be a Dog. During compile time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compilers. At run-time, a Hound is created and assigned to dog. The JVM knows that dog is referring to the object of Hound, so it calls the bark() method of Hound. This is called Dynamic Polymorphism.

4. An Example of Overloading

class Dog{ public void bark(){ System.out.println("woof "); }   //overloading method public void bark(int num){ for(int i=0; i<num; i++) System.out.println("woof "); } }

class Dog{ public void bark(){ System.out.println("woof "); } //overloading method public void bark(int num){ for(int i=0; i<num; i++) System.out.println("woof "); } }

In this overloading example, the two bark method can be invoked by using different parameters. Compiler know they are different because they have different method signature (method name and method parameter list).

References:
1) Defining Method. This tutorial is from Oracle, it explains the components of a method and which of them are used by compiler to differentiate methods.