Static and dynamic binding in java

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-timeWhy 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.

时间: 2024-12-28 22:02:49

Static and dynamic binding in java的相关文章

java之多态(Polymorphic)、动态绑定(Dynamic Binding)、迟绑定(Late Binding)

今天,我们来说说java面向对象最核心的东西,多态.通过多态可以使我们的程序可复用性达到极致,这就是我们为什么要学多态的原因. “多态”(Polymorphic)也叫“动态绑定”(Dynamic Binding)同时也叫“迟绑定”(Late Binding). 动态绑定是指“在执行期间(而非编译期间)判断所引用对象的实际类型,根据其实际类型调用其相应的方法.” 程序代码: public class TestPolymorphic{ public static void main(String a

Static and Dynamic Analysis of Call Chains in Java

Static and Dynamic Analysis of Call Chains in Java

Only Link: What's the difference between dynamic dispatch and dynamic binding

http://stackoverflow.com/questions/20187587/what-is-the-difference-between-dynamic-dispatch-and-late-binding-in-c http://programmers.stackexchange.com/questions/200115/what-is-early-and-late-binding/200123#200123 Only Link: What's the difference betw

static and dynamic lib

Windows下的dll和lib 简介 DLL就是动态链接库 LIB是静态链接库 DLL是程序在运行阶段才需要的文件 LIB是程序编译时需要链接的文件 使用静态库 静态库实际上是obj文件打包而成 使用静态库时 在程序中加入 #pragma comment(lib, "WSock32.lib") 或者通过编译器项目设置中 手动添加Lib静态库 这样在链接程序 就会将静态库链接到程序里面 生成一个可执行文件或其他目标文件 使用动态库 动态库是一个标准的PE文件 经过编译器编译链接的 动态

Build Static and Dynamic Libary in Linux

Build Static Libary ar ru xxx.a *.o ranlib xxx.a//not necessary Build  Dynamic Libary g++ -shared -fPIC -o xxx.so *.o

.Net 中Partitioner static与dynamic的性能对比

先看LINQ的方式,dynamic的方式: void Main() { // testing setup var source = Enumerable.Range(0, 10000000).ToArray(); double[] results = new double[source.Length]; Console.WriteLine("creating partitioner in LINQ way..."); var dt = DateTime.Now; var partiti

浅谈游标选项 Static|Keyset|DYNAMIC|FAST_FORWARD

接好久之前太监的一篇Blog.现在补充几个选项的介绍 所用的语句都是这个 IF OBJECT_ID('T1') IS NOT NULL DROP TABLE T1 GO CREATE TABLE T1 ( ID INT PRIMARY KEY, seq INT ) DECLARE @seq INT=0 WHILE @seq < 50 BEGIN INSERT INTO dbo.T1 ( ID,seq ) VALUES ( @seq+1,@seq) SET @seq=@seq+1 END 1 DE

Dynamic Proxy在Java RMI中的应用

IEEE Spectrum 杂志发布了一年一度的编程语言排行榜,这也是他们发布的第四届编程语言 Top 榜. 据介绍,IEEE Spectrum 的排序是来自 10 个重要线上数据源的综合,例如 Stack Overflow.Twitter.Reddit.IEEE Xplore.GitHub.CareerBuilder 等,对 48 种语言进行排行. 与其他排行榜不同的是,IEEE Spectrum 可以让读者自己选择参数组合时的权重,得到不同的排序结果.考虑到典型的 Spectrum 读者需求

C++ compile Microsoft Visual C++ Static and Dynamic Libraries

出处:http://www.codeproject.com/Articles/85391/Microsoft-Visual-C-Static-and-Dynamic-Libraries