Static and dynamic Binding in Java

Source: Internet
Author: User

Static and dynamic Binding in Java 

Before I explain static and dynamic binding in Java, lets discuss few terms which would help you to understand the Concepts better.

What is reference and object?
Class Human{....}Class Boy Extends Human{ Public Static voidMain( StringArgs[]) { /*this statement simply creates an object of class *boy and assigns a reference of boy to It*/ boy Obj1 = new boy ( );  /* Since Boy extends Human class. The object creation * Can is done on this the. Parent class Reference * can point to a child class Object*/ human o Bj2 = new boy  ();  }}       /span>                
Static and Dynamic Binding in Java

Association of method definition to the method call is known as binding. There is types of binding:static binding and dynamic binding. Lets discuss them one by one.

Static binding or Early binding

The binding which can resolved at compile time by compiler is known as static or early binding. All the static, private and final methods has always been bonded at Compile-time. Why binding of static, final and private methods are always a static binding? You would understand it better after reading dynamic binding. Still let me explain This–compiler knows it all such methods cannot be overridden and would always be accessed by objec T of local class. Hence compiler doesn ' t has any difficulty to determine object of class (local class for sure). That's the reason binding for such methods is static.

Static Binding Example
Class Human{....}Class Boy Extends Human{ Public voidWalk(){ System.Out.println "boy walks" } public static void Main ( string Args[])  { boy obj1 =  boy ();  Obj1.walk (); }}          /span>                

Here we have created a object of boy class and calling the method of the walk() same class. Since Nothing was ambiguous here, compiler would was able to resolve this binding during compile-time, such kind of binding is known as static binding.

Dynamic binding or late binding

When compiler was not able to resolve the call/binding at compile time, such binding was known as Dynamic or late binding. Overriding is a perfect example of the dynamic binding as in overriding both the parent and child classes has same method. Thus while calling the overridden method, the compiler gets confused between parent and child class method (since both the Methods has same name).

Dynamic Binding Example

Here Human are a super class and boy are a child class since boy extends Human. Both These classes has a same method void walk() . Since we have assigned the parent class reference to the child class object, during Call of walk() method the compiler Woul D not being able to decide which walk() method to call. It'll be confused between the walk() method of Human class and method of the boy walk() class. Such kind of bindings is known as dynamic binding as compiler figures out the object type in runtime.

PackageBeginnersbook.Com;Class Human{ Public voidWalk() { System.Out.println("Human Walks"); }}Class Boy Extends Human{ Public voidWalk(){ System.Out.println("Boy walks."); } public  void Main ( string Args[])  {  //reference is of the parent Class human myobj = newboy ();  Myobj.}}          /span>                

Output:

Boy walks
Static Binding vs Dynamic binding

Lets discuss the difference between static and dynamic binding in Java.

    1. The Static binding happens at compile-time while the dynamic binding happens at runtime.
    2. Binding of private, static and final methods always happen at compile time since these methods cannot is overridden. Binding of overridden methods happen at runtime.
    3. Java uses static binding for overloaded methods and dynamic binding for overridden methods.

That's all I had regarding static and dynamic binding in Java. Let me know if you had any questions regarding it.

Static and dynamic Binding in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.