1

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?

theroglu
  • 136
  • 3
  • 12
  • Short answer: tomorrow your boss comes to you and says great job with the OracleModem. Here's a IBM, a Wang, a DEC and a US Robotics modem; let's plug them in and demo it to the CEO! – Elliott Frisch Oct 04 '19 at 07:29
  • in other words, what if instead of `OracleModem` you need to use an `IBMModem` or even, if the program should work with different ones, depending on some settings or start parameters – user85421 Oct 04 '19 at 07:37

0 Answers0