• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static binding and Dynamic binding in OOPS  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding static binding and dynamic bining.

Say for example we have below hierarchy,






1) a.eat(); // Prints Animal eating ---> Static Binding 2) a.eat(); // Prints Dog eating ----> Dynamic Binding

Static Binding means,compiler will be able to decide which method to call based on class type of reference variable at compile time.That is compiler will check whether method is available or not in class.

Dynamic binding means,at runtime JVM will run the method implementaton,based on the object which reference variable is pointing.

So basically compiler will check class type of reference variable and at runtime JVM will check what type of object reference variable is pointng.

Here my my doubt is , in below both cases,that is
1) a.eat(); // Prints Animal eating ---> Static Binding
2) a1.eat(); // Prints Dog eating ----> Dynamic Binding
At compile time,compiler will check whether method is availble or not in class. Since eat() method is available ,then in both cases it should be Static binding. or at run time if JVM decides which method implementation to call,then JVM will check which object the reference variable is pointing,then in above 2 cases also JVM will check in Animal object and Dog object for the method eat().SInce eat() method availble then both should be dynamic binding.

Iam getting doubt on what parameters/conditions we are deciding which is static binding and which is dynamic binding .

Will compiler will check the type of reference variable and also type of object at compile time and when it is ambigious it leaves the decion to JVM?

Or is it like if method call and method implementation belong to same class then it is static binding and if method call and method implementation belong to different class in same inheritance hierarchy then dynamic binding

Corect me if my understading is wrong.

Thanks Chandu
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ShekarChandu Reddy wrote: . . .
1) a.eat(); // Prints Animal eating ---> Static Binding 2) a.eat(); // Prints Dog eating ----> Dynamic Binding

Static Binding means,compiler will be able to decide which method to call based on class type of reference variable at compile time.That is compiler will check whether method is available or not in class.

Dynamic binding means,at runtime JVM will run the method implementaton,based on the object which reference variable is pointing.

So basically compiler will check class type of reference variable and at runtime JVM will check what type of object reference variable is pointng.
Yes
. . .
1) a.eat(); // Prints Animal eating ---> Static Binding
2) a1.eat(); // Prints Dog eating ----> Dynamic Binding
No.
  • 1: Fields, static methods=static binding.
  • 2: Instance methods=dynamic binding.
  • Polymorphism applies to non-static instance methods, full stop. Your eat() method is not private and not static, so it is polymorphic.

    Fields and static methods are always statically bound.

    At compile time,compiler will check whether method is availble or not in class.
    Yes
    Since eat() method is available ,then in both cases it should be Static binding
    No.
    . . . .
    Iam getting doubt on what parameters/conditions we are deciding which is static binding and which is dynamic binding .

    Will compiler will check the type of reference variable and also type of object at compile time and when it is ambigious it leaves the decion to JVM?
    That decision is always left to the JVM. The javac tool checks the declared type (Animal) that the eat method is available. The JVM always checks the runtime type of an object and finds the most overridden type of instance methods dynamically.


    Or is it like if method call and method implementation belong to same class then it is static binding and if method call and method implementation belong to different class in same inheritance hierarchy then dynamic binding
    No. All instance methods are bound dynamically whether there are lots of classes or not. Why check the inheritance hierarchy before executing a method?


    Corect me if my understading is wrong.

    Thanks Chandu
    I am afraid it is.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Start by adding a method to the Dog class, and the Override annotation:-Now compile that. When you declare a variable as Animal it might be a Dog or an Animal or a Cat or a Worm object.The compiler checks at compile‑time that the bark() method is available in Animal (=declared type) and it isn't. Even though Dog has bark(), it will not compile. Because a Worm is an Animal, you must be able to assign that Animal reference to a Worm object.
    d = new Worm();
    And worms don't bark.The compiler cannot tell whether that reference a will be assigned a Dog or a Worm. So it must leave binding of instance methods to the JVM at runtime. Instance methods are found by using the Class<?> object associated with the object in memory. So the most overridden version is found.

    Fields and static methods are not polymorphic. That is why you can get dangerous errors if you try to override them.

    This may differ in other object languages.
     
    Sheriff
    Posts: 4289
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A matter of forum etiquette:

    Please UseCodeTags (←click) when posting code. I did it for you this time. It makes code a lot easier to read.
     
    Shekar Chandu
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Campbell,

    Thank you for giving good explanation on binding concepts. :-).

    I have one more question , if all instance methods fall under dynamic binding,then why overloading is considered to be static binding.

    Thanks
    Chandu
     
    Bartender
    Posts: 1840
    10
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    At compile time, a call gets bound to a specific method signature. This resolves method overloading - if there are multiple methods with the same name, it will choose one of them.
    This can never change. They type is known, the parameter types are known, the method signature is fixed.

    At run time then, therefore it already knows the signature of the method it wants to run. Which version of it in the hierarchy of overridden methods it calls depends upon the type of the actual instance at the time of invoking the method.
    This was unknown until runtime, and can change during execution (if a new value is assigned to a variable). Hence "Dynamic" binding.

    So relating it to the example.

    The static binding determines that it will call the method with signature void eat().
    Dynamic binding determines whether it calls Animal.eat or Dog.eat.


     
    Shekar Chandu
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello,

    Thank you stefan,

    This is what i understood and summarized about static binding and dynamic binding, please do correct me if i went wrong in understanding

    @Overloading, If there are n methods with same name but different arguments,based on method signature compiler will decide which method to execute.
    @Overriding, If there is inheritance hierarchy,then which version of that method will execute is determined at runtime.
    So the type of method to execute is determined at compile time and which method to execute is determined at runtime.




    So in above hierarchy,compiler will check eat() method signature and verify that method exists in Animal class but cannot determine which version of method to call as compiler sees
    same method eat() in Dog class also.
    So basically compiler will check for eat() method in reference variable type Animal and if that method exists in Animal class, compiler also checks in object type to which reference variable is pointing, that is
    Dog and if method exists in Dog class,compiler cannot determine which method to call Animals eat() method or Dogs eat() method and leaves decision to JVM which decides which
    method to be invoked/resolved based on object to which reference variable is pointing.
    This is Dynamic Binding.



    In the above scenario, compiler will check eat() method and signature and verify that method exists in Animal class.Compiler will also check for eat() method in object to which reference
    variable is pointing in this case it is Animal object.Compiler will see that both reference variable type and object type are same and therefore the method call is resolved at compile time.
    This is static binding.

    So compiler checks in class type and object type for method,if method exists in class and object at compile time it is Static binding.(No inheritance)

    And if object type follows inheritance hierarchy,and for same method present in both class type and object type,compiler gets ambigous about resolving method call and leaves decision to JVM.

    And whether it is static binding or dynamic binding, it is all about resolving method calls not method execution, and in both cases static and dynamic binding
    actual method(instance methods) implementation is always at executed at runtime only.

    Thanks
    Chandu
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ShekarChandu Reddy wrote: . . .
    So in above hierarchy,compiler will check eat() method signature and verify that method exists in Animal class but cannot determine which version of method to call as compiler sees
    same method eat() in Dog class also.
    So basically compiler will check for eat() method in reference variable type Animal and if that method exists in Animal class, compiler also checks in object type to which reference variable is pointing, that is
    Dog and if method exists in Dog class,compiler cannot determine which method to call Animals eat() method or Dogs eat() method and leaves decision to JVM which decides which
    method to be invoked/resolved based on object to which reference variable is pointing.
    . . .
    No. The reference is declared as Animal, so the compiler checks that the Animal class has an eat() method. Since a Dog is an Animal, there is no need to check for the eat() method in the Dog class. The compiler “knows” it exists already because of inheritance.

    You are going on about overloading but you have not shown any examples of overloading.The compiler checks whether the Animal class has a makeNoise() method and a makeNoise(boolean) method and decides to call one in line 3 and the other in line 4. That is static binding also called early binding also called compile‑time binding. Maybe the best thing to call it is compile‑time binding. In line 1 the compiler had decided that a Cat is an Animal so it can be assigned to that reference.

    At runtime the JVM decides it is a Cat and calls the Cat version of the method so it does not write Woof! Woof!. There is nothing to stop you getting a different pet:-
    whiskers = new Dog();
    and you will then have different output. That is dynamic binding also called late binding or runtime binding.
     
    Shekar Chandu
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello

    Thank you Campbell,

    As you said,"So the compiler checks that the Animal class has an eat() method. Since a Dog is an Animal, there is no need to check for the eat() method in the Dog class. The compiler “knows” it exists already because of inheritance"

    So it means compiler will check for eat() method in reference variable type and because of inheritance compiler knows eat() method also exists in Dog.But gets ambiguous to resolve which method to call as it exists in both Animal and Dog and leaves decision to JVM.

    Correct me if iam wrong in understanding this point :Compiler checks for method arguments ,signature during compile and knows that eat method exists in both Animal and Dog(Because of inheritance) class,but primarily check for eat method in Animal class and if it exists in Animal class compiler could not bind method call to method implementation and at runtime JVM does the resolving.

    Thanks
    Chandu
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. I don't think ambiguous is the right word, however.
    The javac tool is programmed specifically to leave the binding of instance methods to the JVM at runtime. The JVM can find the method at runtime by finding the Class<T> object associated with any object, and the overridden version of the method will be found there.
     
    Shekar Chandu
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Campbell

    Thank you

    To summarize what i understood from this discussion, StaticBinding and DynamicBinding in Polymorphism is about resolving method calls either at compile time or runtime.
    Actually what i feel/understand is ,the terms StaticBinding and DynamicBinding in Polymorphism are little misleading,as they say about method resolving at compile time or runtime,but
    actually
    all static methods are identified/loaded during compilation-- This is actual Static Binding
    aLL non static methods are invoked by JVM based on object not reference variable type --- This is actual DynamicBinding.

    Whether in static binding or dynamic binding , existence of method is identified depending on arguments of method by compiler based on reference variable type at compile time.
    So existence of method is known to compiler at compile time in both cases of static and dynamic binding.
    But actual method invocation happens at runtime based on object type which reference variable type is pointing in both cases of static and dynamic binding.

    At run time reference variable type a as shown below can take many forms by pointing to different objects which pass IS-A condition,a can take any form of dog/cat/dinosaur,which form it takes can be known only at run time based on object created and pointed to reference variable.This is polymorphism.

    Animal a;
    a = new Dog();
    a = new Cat();
    a = new Dinosaur();

    In order to support polymorphism java has been designed to invoke methods at runtime based on objects not on reference variable type.
    If method identified at compile time is invoked at runtime, then polymorphism would be lost.

    Also in case of overloaded methods,if a class has 10 overloaded methods,compiler can easily identify which method to execute based on arguments of method.
    So at a given point of time there would be one method that match arguments.(But not sure whether overloading can be called as static polymorphism/static binding,as identification of method is anyway by done by compiler even if there is no inheritance)

    In case of overriding there will be only one method that exists in super class which is overrided in subclass.Compiler checks for existence of methods at compile time,if exist compilation passes or else fails.

    Whether overloading or overriding,JVM will invoke and execute methods at runtime.

    please correct me if iam wrong in my understanding

    Thanks
    chandu
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not sure I agree about the terms being misleading. They are jargon terms and one has to learn their meanings.
    Also the compiler can easily determine which version of an overloaded method to call, statically, but the user may be mistaken about which will be called.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!