Wednesday, August 30, 2017

Polymorphism | Binding - OverLoading vs OverRidding

Polymorphism is the ability for data to be processed in more than one form. It allows the performance of the same task in various ways. It consists of method overloading and method overriding, i.e., writing the method once and performing a number of tasks using the same method name.

Connecting a method call to the method body is known as binding.

Static binding/Early binding/Compile time binding :-
When type of the object is determined at compiled time(by the compiler), it is known as static binding.

class Dog{ 
 private void eat(){System.out.println("dog is eating...");}
 public static void main(String args[]){ 
  Dog d=new Dog(); 
  d.eat(); 
 } 
}

OR

The binding which can be resolved at compile time by compiler is known as static or early binding. Binding of all the static, private and final methods is done at compile-time .

Static binding is better performance wise (no extra overhead is required). Compiler knows that all such methods cannot be overridden and will always be accessed by object of local class. Hence compiler doesn’t have any difficulty to determine object of class (local class for sure). That’s the reason binding for such methods is static.

public class NewClass
{
    public static class superclass
    {
        static void print()
        {
            System.out.println("print in superclass.");
        }
    }
    public static class subclass extends superclass
    {
        static void print()
        {
            System.out.println("print in subclass.");
        }
    }

    public static void main(String[] args)
    {
        superclass A = new superclass();
        superclass B = new subclass();
        A.print();
        B.print();
    }
}

Output:-
print in superclass.
print in superclass.

Since the print method of superclass is static, compiler knows that it will not be overridden in subclasses and hence compiler knows during compile time which print method to call and hence no ambiguity.







Dynamic binding/Late binding/Run time binding :-When type of the object is determined at run-time, it is known as dynamic binding. 

class Animal{ 
 void eat(){System.out.println("animal is eating...");} 

 
class Dog extends Animal{ 
 void eat(){System.out.println("dog is eating...");} 
 
 public static void main(String args[]){ 
  Animal a=new Dog(); 
  a.eat(); 
 } 
}

In Dynamic binding compiler doesn’t decide the method to be called. Overriding is a perfect example of dynamic binding. In overriding both parent and child classes have same method . Let’s see by an example

public class NewClass
{
    public static class superclass
    {
        void print()
        {
            System.out.println("print in superclass.");
        }
    }

    public static class subclass extends superclass
    {
        @Override
        void print()
        {
            System.out.println("print in subclass.");
        }
    }

    public static void main(String[] args)
    {
        superclass A = new superclass();
        superclass B = new subclass();
        A.print();
        B.print();
    }
}

Output:-
print in superclass.
print in subclass.

- Methods are not static in this code.
- During compilation, the compiler has no idea as to which print has to be called since compiler goes only by referencing variable not by type of object and therefore the binding would be delayed to runtime and therefore the corresponding version of print will be called based on type on object.


OverLoading vs OverRidding - Both are concepts of polymorphism.
- Method overloading is a form of static binding. Method overriding is a form dynamic binding.
- Overloading is applied in single class, but overriding is applicable for inherited class.
- Method overloading is always specific to method signature. It defines number of parameter, type of parameter and sequence of parameter.

No comments:

Post a Comment

Web Development

Design Phase:- Below all these represent different stages of the UX/UI design flow:- Wireframes represent a very basic & visual repr...