简体   繁体   中英

Why do we store object in an interface reference?

I have a doubt about interfaces.
I have seen some code,they are writing

Connection con = Drivermanager.getConnection("URL","User name","Pwd");

here con (reference varible of Connection Interface ) holding Oracle Connection Object. since we know that we are creating Oracle Connection Object why cant we write

OracleConnectionClass Oraclecon = Drivermanager.getConnection("URL","User name","Pwd");

if i asked some people they say that ,they are achieving polymorphism,by using Connection Interface.

But,
what is the use.
we can do

create statement,execute query.. etc by both references (con and Oraclecon).

simillarly for List and ArrayList
Thanks in advance

You know the exact type you are creating right now, but that may change in the future due to your own code changing, or the implementation changing.

Polymorphism is used to abstract away details so you don't have to touch code later. If you used an exact type instead of an interface here, and that type changed, you'd have to change your code.

The consequences of this seem obvious. Changing code is work, work cost money, therefor less change means less cost.

But its even more expensive. Each time you change stable code you risk introducing a bug. That risk is low here but also worth mentioning.

This is why they use an interface.

Here's a good example of polymorphism's power.

Queue<Float> queue = new LinkedList<Float>();

We can cause a big change the behavior simply.

Queue<Float> queue2 = new BlockingQueue<Float>();

Or how about this?

Queue<Float> queue3 = new PriorityBlockingQueue<Float>();

All are queues we can use to change behavior quickly.

A short answer would be "because we don't care that it's an OracleConnection". It could be changed to a MySQLConnection and the code would still work, since both would implement the Connection class.

Another example is the List interface.

List<String> l = new ArrayList<String>();

If you need to change the list type, you need to change only one place.

List<String> l = new LinkedList<String>();

In a code written tightly coupled it achieves nothing but fancy names like I used polymorphism.

But imagine a case when You have an instance variable as

Connection con;

and a parameterised constructor accepting Connection

public businessDao(Connection con){
this.con = con;
}

//some function using connection

Now the constructor can accept any type of connection object that implements Connection interface be it a

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance() 

or a jndi connection as follows

dataSource = (DataSource) context.lookup("java:comp/env/jdbc/jelasticDb");
con = dataSource.getConnection();

you dont need to hard code your code with the type of connection here just pass the required type of object.

You can find actual use of polymorphism in Dependency Injection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM