Overriding vs. Overloading in Java

overloading-vs-overriding

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();
    }
}

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 ");
    }
}

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.

Category >> Basics  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Great article! You have well done the difference between Overriding and Overloading in Java which is very important concepts in Java. I am also java developer suddenly I got confused in java overriding concept. Across your article, you made it very clear and easy to understand the concept. Which is having detailed information with related images and explanation about overriding and overloading in java. Thank you for this wonderful article.

  • Fatima Mohammed

    thank you so much.. you made it very clear and easy to understand

  • Thejus Rao

    Author is right. Once we override a function then it cannot be used in that class. In that sense, it’s not polymorphism as it requires something to do 2 separate functions. That won’t be the case if we use function overriding.

  • Sudhir

    Polymorphism applies to overriding, not to overloading —- this statement wrong.
    Polymorphism applies to both overriding and overloading —- this statement correct one.

  • Dipta Dip


    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 ");
    }
    }

  • Thank you very much for this, I now know more about programming. Keep it up!

  • william

    overloading can happens in super class and sub class.
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.9

  • JAC_JustAnotherCoder

    thanks 🙂

  • Prince Dudley

    This is a very nice article that demonstrates the difference.

  • Prasad Biradar

    single action can be performed by many ways….
    2 types
    1.compile time-overloding
    2 run time-over riding

  • ansaik

    very easy but one question.
    what is Polymorphism

  • Allen Scott
  • SANAJIT GHOSH

    very nice picture to explain

  • Mark Loh

    method overloading is the process of defining more than one function in a class with the same name but different argument lists. at the line of function calling, the complier will invoke the correct one by using the type and member of the argument.

  • ling maaki
  • Elvin Mirze

    Great!

  • Pingback: 免费Simple Java (非常简单的英文) | | Evolution Unit 进化Evolution Unit 进化()

  • Pingback: Simple Java Questions | Need 2 Remember()

  • Pingback: Java 百问 from programcreek - Dylan‘s Zone()