Java Dynamic Binding

  1. Create a Simple Dynamic Binding in Java
  2. Create a Simple Static Binding in Java
  3. Complex Dynamic Binding in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Create a Simple Dynamic Binding in Java


Bind_Dynamic.java

public class Bind_Dynamic {
	
	protected String val;
	
	void display (String str) {
		
		val = "Base Class Fuction ".concat(str);
		
		System.out.println(val);
	}
}
 
class SubClass extends Bind_Dynamic{
	
	void display (String str) {
				
		if(val == null) 
		{
		
			str = "Derived Class Fuction ".concat(str);
			
			System.out.println(str);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		
		obj.display("Called");
	}
}

Sample Output

Derived Class Fuction Called

Now rewrite the Code in SubClass

class SubClass extends Bind_Dynamic{
	
	void display (String str) {
		
		super.display(str);
		
		if(val == null) 
		{
		
			str = "Derived Class Fuction ".concat(str);
			
			System.out.println(str);
		}
	}
}

Now our Sample Output

Base Class Fuction Called




B. Create a Simple Static Binding in Java


Bind_Static.java

public class Bind_Static {
	
	void display(String str) {
		
		System.out.println(str);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
	
		Bind_Static obj = new Bind_Static();
		
		obj.display("This is called Static Binding..");
	}
}

Sample Output

This is called Static Binding..




C. Complex Dynamic Binding in Java


Bind_Ex1.java

public class Bind_Ex1 {
 
	String text = "Bind_Ex1's";
	
	void display() {
		
		System.out.println(text + " function called");
	}
}
class SubClass1 extends Bind_Ex1 {
	
	void display() {
		
		super.display();
		
		text = "SubClass1's";
		System.out.println(text + " function called");
	}
}
class SubClass2 extends SubClass1 {
	
	void display() {
		
		super.display();
		
		text = "SubClass2's";
		System.out.println(text + " function called");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass2 obj = new SubClass2();
		
		obj.display();
	}
}

Sample Output

Bind_Ex1's function called
SubClass1's function called
SubClass2's function called






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com