Follow us on Facebook

Header Ads

Welcome to JAVA POint

Static and Dynamic building in JAVA

                             Static and Dynamic building in JAVA


Relationship of strategy call to the technique body is known as restricting. There are two kinds of restricting: Static Binding that occurs at arrange time and Dynamic Binding that occurs at runtime. Before I clarify static and dynamic restricting in java, lets see not many terms that will assist you with understanding this idea better. 



What is reference and article? 


class Human{ 


.... 



class Boy expands Human{ 


public static void primary( String args[]) { 


/*This articulation essentially makes an object of class 


*Boy and doles out a reference of Boy to it*/ 


Kid obj1 = new Boy(); 


/* Since Boy expands Human class. The article creation 


* should be possible thusly. Parent class reference 


* can have kid class reference appointed to it 


*/ 


Human obj2 = new Boy(); 


Static and Dynamic Binding in Java 


As referenced above, relationship of strategy definition to the technique call is known as restricting. There are two kinds of restricting: Static restricting and dynamic restricting. Lets examine them. 


Static Binding or Early Binding 


The limiting which can be settled at arrange time by compiler is known as static or early restricting. The limiting of static, private and last strategies is assemble time. Why? The explanation is that the these strategy can't be superseded and the kind of the class is resolved at the assemble time. Lets see a guide to get this: 


Static restricting model 


Here we have two classes Human and Boy. Both the classes have same strategy walk() however the technique is static, which implies it can't be overriden so despite the fact that I have utilized the object of Boy class while making object obj, the parent class strategy is called by it. Since the reference is of Human kind (parent class). So at whatever point a limiting of static, private and last techniques occur, kind of the class is dictated by the compiler at assemble time and the limiting happens without further ado. 

class Human{ 

public static void walk() 

System.out.println("Human strolls"); 

class Boy expands Human{ 

public static void walk(){ 

System.out.println("Boy strolls"); 


public static void primary( String args[]) { 

/* Reference is of Human sort and item is 


* Boy type 


*/ 

Human obj = new Boy(); 

/* Reference is of HUman kind and article is 

* of Human sort. 

*/ 

Human obj2 = new Human(); 

obj.walk(); 

obj2.walk();

Yield: 

Human strolls 

Human strolls 

Dynamic Binding or Late Binding 


At the point when compiler can't resolve the call/restricting at incorporate time, such restricting is known as Dynamic or late Binding. Technique Overriding is an ideal illustration of dynamic restricting as in abrogating both parent and youngster classes have same strategy and for this situation the kind of the article figures out which strategy is to be executed. The kind of article is resolved at the run time so this is known as powerful restricting. 

Dynamic restricting model 


This is the very model that we have seen previously. The lone contrast here is that in this model, abrogating is really occurring since these strategies are not static, private and last. If there should arise an occurrence of superseding the call to the overriden technique is resolved at runtime by the sort of article along these lines late restricting occurs. Lets see a guide to get this: 

class Human{ 

/Overridden Method 

public void walk() 

System.out.println("Human strolls"); 

class Demo expands Human{ 

/Overriding Method 

public void walk(){ 

System.out.println("Boy strolls"); 

public static void primary( String args[]) { 

/* Reference is of Human sort and item is 


* Boy type 


*/ 


Human obj = new Demo(); 


/* Reference is of HUman sort and item is 


* of Human sort. 


*/ 


Human obj2 = new Human(); 


obj.walk(); 


obj2.walk(); 




Yield: 


Kid strolls 


Human strolls 


As you can see that the yield is unique in relation to what we found in the static restricting model, in light of the fact that for this situation while formation of article obj the sort of the item is resolved as a Boy type so strategy for Boy class is called. Recollect the kind of the item is resolved at the runtime. 


Static Binding versus Dynamic Binding 


Lets talk about the contrast among static and dynamic restricting in Java. 


Static restricting occurs at arrange time while dynamic restricting occurs at runtime. 


Restricting of private, static and last techniques consistently occur at assemble time since these strategies can't be superseded. At the point when the technique abrogating is really occurring and the reference of parent type is appointed to the object of youngster class type at that point such restricting is settled during runtime. 


The limiting of over-burden techniques is static and the limiting of abrogated strategies is dynamic.

0 Comments:

Post a Comment

C language and C++ progaming