java newbie
java newbie

Reputation: 407

How does JVM differentiates between method overloading and method overriding internally?

I would like to know how does JVM differentiates between method overloading and method overriding internally.

Upvotes: 5

Views: 3237

Answers (4)

Naresh Joshi
Naresh Joshi

Reputation: 4557

Method overloading is handled by the compiler (that's why known as static binding) while overriding is handled by JVM.

Overloaded methods are different and can be recognized separately based on the argument list, that's why they get resolved by the compiler at compile time.

Method overriding occurs when we try to call a overridden method on the parent reference which is holding child object. Because we are calling the method on parent reference so at compile time method get resolved from parent reference type so, for the compiler, the method of parent is getting called. But parent is holding the object of child so at runtime method gets called from child.

In order to understand how Java (Compiler + JVM) handles both things internally let's take an example of Mammal and Human class

public class OverridingInternalExample {

    private static class Mammal {
        public void speak() { System.out.println("ohlllalalalalalaoaoaoa"); }
    }

    private static class Human extends Mammal {

        @Override
        public void speak() { System.out.println("Hello"); }

        // Valid overload of speak
        public void speak(String language) {
            if (language.equals("Hindi")) System.out.println("Namaste");
            else System.out.println("Hello");
        }

        @Override
        public String toString() { return "Human Class"; }

    }

    //  Code below contains the output and bytecode of the method calls
    public static void main(String[] args) {
        Mammal anyMammal = new Mammal();
        anyMammal.speak();  // Output - ohlllalalalalalaoaoaoa
        // 10: invokevirtual #4 // Method org/programming/mitra/exercises/OverridingInternalExample$Mammal.speak:()V

        Mammal humanMammal = new Human();
        humanMammal.speak(); // Output - Hello
        // 23: invokevirtual #4 // Method org/programming/mitra/exercises/OverridingInternalExample$Mammal.speak:()V

        Human human = new Human();
        human.speak(); // Output - Hello
        // 36: invokevirtual #7 // Method org/programming/mitra/exercises/OverridingInternalExample$Human.speak:()V

        human.speak("Hindi"); // Output - Namaste
        // 42: invokevirtual #9 // Method org/programming/mitra/exercises/OverridingInternalExample$Human.speak:(Ljava/lang/String;)V
    }
}

When we compile above code and try to look at the bytecode using javap -verbose OverridingInternalExample, we can see that compiler generate a constant table where it assigns integer codes to every method call.

java method constant pool

I have included the bytecode along with above method calls and by looking at above code we can see that the bytecodes of humanMammal.speak() , human.speak() and human.speak("Hindi") are totally different because the compiler is able to differentiate between them based on the argument list and class reference. And this is why Method Overloading is known as Static Polymorphism or Static Binding.

But bytecode for anyMammal.speak() and humanMammal.speak() is same because according to compiler both methods are called on Mammal reference but the output for both method calls is different because at runtime JVM knows what object the reference is holding and JVM calls the method on the object and this is why Method Overriding is known as Dynamic Polymorphism or simply Polymorphism.

If you want to know more about this you can read more on How Does JVM Handle Method Overloading and Overriding Internally.

Upvotes: 1

Atsby
Atsby

Reputation: 2327

The JVM only deals with method overriding. A method is overridden by adding a method with the same signature in a derived class (the only allowed difference is in the return type, which is allowed to be more specific). The signature encodes the method's name, as well as the types of the parameters and the return type.

Method overloading means having multiple methods with the same "simple name" but different signatures. At compile time, the javac compiler chooses one of the same-named methods based on the types of the arguments and places its signature in the compiled .class file. A method invocation in compiled Java bytecode must specify the signature of the callee.

Upvotes: 5

Raj
Raj

Reputation: 1965

Jvm in not involved in method overloading. So it does not have to differentiate between method overloading and method overriding.

You would understand this clearly if you understand how method overloading and method overriding is implemented by Java behind the scenes.

Before going into the difference between overloading and overriding , let us first take a look into Polymorphism .

What is polymorphism ?

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations.

Different types of polymorphism in java. 1) Method Overloading 2) Method Overriding

Types of polymorphism in java: Static, Dynamic, Runtime and Compile time

Now let us come back to the original question and understand the difference between overloading and overriding.

1. The first and most important is that method overloading a compile time polymorphism while method overriding is run time polymorphism. So JVM has to only deal with method overriding . Method overloading is already resolved after compilation.

2.In Method Overloading, methods must have different signature.In Method Overriding, methods must have same signature.

There are many more differences, but above two can really differentiate method overloading from method overriding.

Before winding up let us also look at what each is trying to achieve .

Method Overloading is to “add” or “extend” more to method’s behavior.Method Overriding is to “Change” existing behavior of method.

Upvotes: 2

Viraj Nalawade
Viraj Nalawade

Reputation: 3227

Though name of method remains same in case of both method overloading and overriding, main difference comes form the fact that method overloading is resolved during compile time, while method overriding is resolved at runtime. Also rules of overriding or overloading a method is different in Java. For example, private, static and final method cannot be overriding in Java but you can still overload them. For overriding both name and signature of method must remain same, but in for overloading method signature must be different. Last but not the least difference between them is that call to overloaded methods are resolved using static binding while call to overridden method is resolved using dynamic binding in Java. SO JVM is not involved in overloading
Method overloading:

class Calculation{  
  void sum(int a,int b){System.out.println(a+b);}  
  void sum(int a,int b,int c){System.out.println(a+b+c);}   
}  

Method Overriding:

class Calculation{  
void sum(){System.out.println("sum in superclass");}  
}  
class Specific extends Calculation{  
void sum(){System.out.println("sum in Specific");}
}

Upvotes: 0

Related Questions