ContactUs :

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

Example of Dynamic Binding in Java

In last section we have seen example of static binding which clears things that static binding occurs on compile time and Type information is used to resolve methods. In this section we will see example of dynamic binding in java which occurs during run time and instead of Type or Class information, Object is used to resolve method calls.

 

public class DynamicBindingTest {

 

    public static void main(String args[]) {

        Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car

        vehicle.start();       //Car's start called because start() is overridden method

    }

}

 

class Vehicle {

 

    public void start() {

        System.out.println("Inside start method of Vehicle");

    }

}

 

class Car extends Vehicle {

 

    @Override

    public void start() {

        System.out.println("Inside start method of Car");

    }

}

 

Output:

Inside start method of Car

 

In this example of Dynamic Binding we have used concept of method overriding. Car extends Vehicle and overrides its start() method and when we call start() method from a reference variable of type Vehicle, it doesn't call start() method from Vehicle class instead it calls start() method from Car subclass because object referenced by Vehicle type is a Car object. This resolution happens only at runtime because object only created during runtime and called dynamic binding in Java. Dynamic binding is slower than static binding because it occurs in runtime and spends some time to find out actual method to be called.

Inheritance

1) Super class reference variable can point to Sub Class Object e.g.

 

SuperClass parent = new SubClass();

 

is legal at compile time because of IS-A relationship between Super class and Sub Class. In Java Sub Class IS-A Super class like Mango IS-A Fruit.

 

On the other hand if an you want to store object of Sub class, which is stored in super class reference variable, back on Sub class reference variable you need to use casting in Java, as shown below :

 

SubClass child = (SubClass) parent; //since parent variable pointing to SubClass object

 

If you do not want to allow Inheritance for your class than you can make it final.  final classes can not be extended in Java and any attempt to inherit final class will result in compile time error.

 

Constructor in Java are not inherited by Sub Class. In face Constructors are chained, first statement in constructor is always a call to another constructor, either implicitly or explicitly. If you don't call any other constructor compiler will insert super(), which calls no argument constructor of super class in Java. this keyword represent current instance of class and super keyword represent instance of super class in Java.

 

Private members of Super class is not visible to Sub class even after using Inheritance in Java. Private members include any private field or method in Java.

 

Java has a special access modifier known as protected which is meant to support Inheritance in Java. Any protected member including protected method and field are only accessible in Child class or Sub class outside the package on which they are declared.

 

Use @Override annotation while overriding super class's method in subclass. This will ensure a compile time check on whether overriding method actually overrides super class method or not.

Related Posts Plugin for WordPress, Blogger...
Flag Counter