Skip to content

Instantly share code, notes, and snippets.

@a7medfahmy94
Created December 24, 2014 08:53
Show Gist options
  • Save a7medfahmy94/4ce91d7f5300fe664288 to your computer and use it in GitHub Desktop.
Save a7medfahmy94/4ce91d7f5300fe664288 to your computer and use it in GitHub Desktop.
Static VS Dynamic Binding in Java
import java.util.*;
import java.lang.*;
import java.io.*;
class A{
public void print(){
System.out.println("A is called");
}
}
class B extends A{
public void print(){
System.out.println("B is called");
}
}
class StaticDynamicBinding
{
public static void fun(A obj){
System.out.println("A is called");
obj.print(); //this is dynamic binding,print will be called based on the type
//it may be A or B
}
public static void fun(B obj){
System.out.println("B is called");
obj.print();
}
public static void main (String[] args) throws java.lang.Exception
{
A obj = new B();
fun(obj);//this is static binding, will call fun(A) because obj is of type A
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment