Java: late binding and where i use it? What's benefit?

651 views Asked by At

I'm trying to understand late binding. And searched results: Late Binding: type is unknown until the variable is exercised during run-time; usually through assignment but there are other means to coerce a type; dynamically typed languages call this an underlying feature, but many statically typed languages have some method of achieving late binding.

and example is like this:

 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");
    }
}

But what's benefit Vehicle vehicle = new Car(); using this. Should just write Car car = new Car(); ? Please explain to me?

2

There are 2 answers

0
Stephen C On BEST ANSWER

But what's benefit Vehicle vehicle = new Car(); using this. Should just write Car car = new Car(); ? Please explain to me?

Yes you could do that. In this tiny example, there is little immediate benefit in late binding.

But what about this slightly different example?

public static void main(String args[]) {
    Vehicle vehicle = (args[0].equals("car")) ? new Car() : new Vehicle();
    vehicle.start(); 
}

... or if we add more subclasses of Vehicle, and (say) instantiate a Car or a Truck ...


There are other benefits too, that will become clearer to you as you gain experience in programming, especially as you have to write and maintain larger programs.


For the record, Java is generally classified as a statically typed language. And the polymorphism / late binding in both your example and mine is relying on static typing. Dynamic typing only comes into play if we use type casts, instanceof and similar.

The distinction between static and dynamic typing is about when the type checking happens. If the type checking happens (mostly) at compile time and the errors are reported at compile time, then the language is statically checked.


We could also add more subclasses of Vehicle, and modify the main method to create and start any kind of vehicle, without mentioning the names of the subclasses in the source code for main. That's another aspect of late binding that is not evident in your example.

0
Dheeraj Joshi On

The method to be called is decided at runtime, in every case here, based on the runtime type of the object. The only decisions made at compile time are for calls to final, private, or static methods, or choices among a set of overloaded methods (which could still lead to runtime choices if the overloaded methods are not final, private, or static.)

And the answer to the second question: why ?

 vehicle car= new Car();

and not

   Car car= new Car();

This is polymorphism and thinks of a situation when you have to use a for loop with car and bike.They both are vehicle so here we will make a vehicle array and can store both the objects and we don't have to use bike and car loops differently.