Pages

Monday, June 17, 2013

Static Binding VS Dynamic Binding



   Hope you are familiar with the Fish tank example by now.If I brief it a little we created a Fish class which is the parent class or the super class of several other sub classes namely,TankCleaner class,GoldFish class. and we demonstrated our fish tank using the FishDemo class.Thanks to the concept inheritance in OOP, we can extend the super class and inherit its methods and attributes to its sub classes.

So in this way, we can create an object like this.
             ex [1]-----> Fish f1=new Fish();

here, our object name is f1, and it's type of a typical GoldFish.and its reference type also GoldFish.
             ex[2]-------> Fish f2=new GoldFish();
here our object name is f2 and its object type(instance type) is GoldFish, while it references Fish.

           Using the concept overriding, it is possible to insert the  methods available in Fish
class, to its subclasses. Overriding helps us to increase the usability by implementing the same method in different ways. when overriding we have to have the same method signature. [method name and input parameters.return types are not included into method signature.]
  Think about a method available in Fish class called,swim().
               public void swim(){
                           System.out.print("Fish is swimming");
                              }

     And think that there is an overrided method in GoldFish class like this.

              public void swim(){
                           System.out.println("GoldFish is swimming");
                            }

lets move on to our todas topic. if a "f1" object is created as ex[1], and we call the swim method for it like this what will happen??
                           f1.swim();
obviously it will return            Fish is swimming

But think about     f2.swim();  
                   You will se that it will return         GoldFish is swimming


Lets digg into the theory behind this. when running  the second example, at the compile time, the compiler has to think about 2 options whether  to implement the super class's implementation or the sub class's implementation.Actually the the compiler does not decide anything  and it leaves it to be decided at the run time.Since the object is considered as as a real world object, it should be implemented in the real world way.this is decided at the run time. This binding is called , Dynamic binding.

Dynamic binding happens only for methods which are inherited from super classes and overridden in the sub classes.Such method calls are bound, based on their actual object type.

Static binding
If the compiler can resolve the binding at the compile time only then such a binding is called Static Binding or Early Binding.All the static method calls are resolved at compile time itself and hence we have static binding for static method calls.
        Similarly, access to all the member variables in Java follows static binding as Java doesn't support (in fact, it discourages) polymorphic behavior of member variables.
So it is worth to have a clear understanding, that variables are bound at the compilation time.

                                                                         Achini
                                                             



Don't forget to become a follower of SeethaGangula.!!!

No comments:

Post a Comment