Created
December 24, 2014 08:53
-
-
Save a7medfahmy94/4ce91d7f5300fe664288 to your computer and use it in GitHub Desktop.
Static VS Dynamic Binding in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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