Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you declare an Interface and then instantiate an object with it in Java?

Tags:

A friend and I are studying Java. We were looking at interfaces today and we got into a bit of an discussion about how interfaces are used.

The example code my friend showed me contained this:

IVehicle modeOfTransport1 = new Car(); IVehicle modeOfTransport2 = new Bike(); 

Where IVehicle is an interface that's implemented in both the car and bike classes. When defining a method that accepts IVehicle as a parameter you can use the interface methods, and when you run the code the above objects work as normal. However, this works perfectly fine when declaring the car and bike as you normally would like this:

Car modeOfTransport1 = new Car(); Bike modeOfTransport2 = new Bike(); 

So, my question is - why would you use the former method over the latter when declaring and instantiating the modeOfTransport objects? Does it matter?

like image 223
mal Avatar asked Oct 10 '11 17:10

mal


People also ask

Why do we declare interface in Java?

Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. It is also used to achieve loose coupling.

Why do we create object instance from interface instead of class?

It basically makes your code flexible and dynamic rather than rigid and tightly coupled.

Can interface be instantiated in Java?

Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class.

What is the purpose of interfaces in Object Oriented Programming?

In object-oriented programming, an interface allows you to specify a set of function signatures and hide the implementation of those functions in an "implementing" class. Interfaces form a contract between the class and the outside world.


1 Answers

There is a big plus on declaring them using the interface, which is what is known as "coding to an interface" instead of "coding to an implementation" which is a big Object Oriented Design (OOD) principle, this way you can declare a method like this:

public void (IVehicle myVehicle) 

and this will accept any object that implements that interface, then at runtime it will call the implementation like this:

public void (IVehicle myVehicle) {     myVehicle.run() //This calls the implementation for that particular vehicle. } 

To answer the original question, why would you use one over the other there are several reasons:

1) Declaring them using an interface, means you can later substitute that value with any other concrete class that implements that interface, instead of being locked into that particular concrete class

2) You can take full advantage of polymorphism by declaring them using an interface, because each implementation can call the correct method at runtime.

3) You follow the OOD principle of code to an interface

like image 72
Oscar Gomez Avatar answered Sep 28 '22 14:09

Oscar Gomez