I have a conceptual question regarding usage of interface variables.
Lets say we have an interface called Modem
interface Modem {
public boolean open();
public boolean close();
public int read ();
public int write(byte[] buffer);
}
Also we have a class called OracleModem,
public class OracleModem implements Modem {
public boolean open() {
// implementation
}
public boolean close() {
// implementation
}
public int read() {
// implementation
}
public int write(byte[] buffer) {
// implementation
}
}
Now the question I have is what is the benefit of using below code rather than the following one?
Modem modem = new OracleModem();
modem.open();
modem.write(buffer);
modem.read();
modem.close();
and
OracleModem modem = new OracleModem();
modem.open();
modem.write(buffer);
modem.read();
modem.close();
I am a junior developer and it just so often happens that I see the object reference is assigned to the interface value and used as such. What is the benefit of doing it compared to instantiating a regular object representation and reaching it as normal?