Static and dynamic binding in java

來源:互聯網
上載者:User

標籤:

Static and dynamic binding in java 

Before I explain static and dynamic binding in java, lets discuss few terms which will help you to understand the concepts better.

 What is reference and object?
class Human{....}class Boy extends Human{   public static void main( String args[]) {       /*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 be done in this way. Parent class reference         * can point to a child class object*/       Human obj2 = new Boy();   }}
Static and Dynamic Binding in Java

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

Static Binding or Early Binding

The binding which can be resolved at compile time by compiler is known as static or early binding. All the static, private and final methods have always been bonded at compile-time. Why binding of Static, final and private methods is always a static binding? You would understand it better after reading dynamic binding. Still let me explain this – Compiler knows that all such methods cannot be overridden and will always be accessed by object of local class. Hence compiler doesn’t have 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 void walk(){      System.out.println("Boy walks");   }   public static void main( String args[]) {      Boy obj1 = new Boy();      obj1.walk();   }}

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

Dynamic Binding or Late Binding

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

Dynamic binding example

Here Human is a super class and Boy is a child class since Boy extends Human. Both these classes have 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 would not be able to decide which walk() method to call. It will be confused between the walk()method of Human class and walk() method of Boy class. Such kind of bindings are known as dynamic binding as compiler figures out the object type in runtime.

package beginnersbook.com;class Human{   public void walk()   {       System.out.println("Human walks");   }}class Boy extends Human{   public void walk(){       System.out.println("Boy walks");   }   public static void main( String args[]) {       //Reference is of parent class       Human myobj = new Boy();       myobj.walk();   }}

Output:

Boy walks
Static Binding vs Dynamic Binding

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

  1. Static binding happens at compile-time while dynamic binding happens at runtime.
  2. Binding of private, static and final methods always happen at compile time since these methods cannot be 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 have regarding static and dynamic binding in java. Let me know if you have any questions regarding it.

Static and dynamic binding in java

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.