What are Interfaces and When to use Interfaces?
Interfaces:
A good place to look at Java Interfaces would be the collections framework.
java.util.List //interface
java.util.ArrayList //Concrete class
java.util.LinkedList //Concrete class
So you can write code like this:
List l = new ArrayList();
l.add(..)
//do something else.
If in future you want to change the implementation with say LinkedList or you own AwesomeList which implements List interface, all you have to do is change the very first line to:
List l = new MyAwesomeList();
or
List l = new LinkedList();
The rest of the code would follow through.
More Examples:
Use interfaces to define an application programming contract (blueprint, interface) which "3rd-party" vendors have to fully adhere and implement. This way the endusers can just code against the API contract and easily switch of the concrete implementation "under the hoods" without changing the code.
The JDBC API is an excellent example. It exist of almost only interfaces. The concrete implementations are provided as "JDBC drivers". This enables you to write all the JDBC code independent of the DB vendor. You can just change the JDBC driver without changing any line of Java code (expect of any hardcoded DB-specific SQL code) whenever you'd like to switch of DB vendor.
Another example is the Java EE API, it also contains pretty much interfaces and abstract classes. The concrete implementations are provided as "Java EE application servers", "Servletcontainers", etc, such as Sun Glassfish, Apache Tomcat, etc. This enables you to deploy the webapplication (WAR) to whatever Java web server you like.
When to go for Interfaces?
Interfaces are needed where you expect volatility in your program, points at which you anticipate change, points where your design needs to bend.
Implementation is fragile in this sense: it breaks quite easily. This is why subclassing isn't always the best solution, just as long-winded methods that implement some complicated behavior all by themselves are generally a bad idea.
Interfaces are more flexible and can deal with a lot more stress on the design of your program than implementation.
By introducing interfaces into your program, you really introduce points of variation at which you can plug in different implementations for that interface. Interfaces' primary purpose is abstraction, decoupling the "what" from the "how".
Source: StackOverflow
A good place to look at Java Interfaces would be the collections framework.
java.util.List //interface
java.util.ArrayList //Concrete class
java.util.LinkedList //Concrete class
So you can write code like this:
List l = new ArrayList();
l.add(..)
//do something else.
If in future you want to change the implementation with say LinkedList or you own AwesomeList which implements List interface, all you have to do is change the very first line to:
List l = new MyAwesomeList();
or
List l = new LinkedList();
The rest of the code would follow through.
More Examples:
Use interfaces to define an application programming contract (blueprint, interface) which "3rd-party" vendors have to fully adhere and implement. This way the endusers can just code against the API contract and easily switch of the concrete implementation "under the hoods" without changing the code.
The JDBC API is an excellent example. It exist of almost only interfaces. The concrete implementations are provided as "JDBC drivers". This enables you to write all the JDBC code independent of the DB vendor. You can just change the JDBC driver without changing any line of Java code (expect of any hardcoded DB-specific SQL code) whenever you'd like to switch of DB vendor.
Another example is the Java EE API, it also contains pretty much interfaces and abstract classes. The concrete implementations are provided as "Java EE application servers", "Servletcontainers", etc, such as Sun Glassfish, Apache Tomcat, etc. This enables you to deploy the webapplication (WAR) to whatever Java web server you like.
When to go for Interfaces?
Interfaces are needed where you expect volatility in your program, points at which you anticipate change, points where your design needs to bend.
Implementation is fragile in this sense: it breaks quite easily. This is why subclassing isn't always the best solution, just as long-winded methods that implement some complicated behavior all by themselves are generally a bad idea.
Interfaces are more flexible and can deal with a lot more stress on the design of your program than implementation.
By introducing interfaces into your program, you really introduce points of variation at which you can plug in different implementations for that interface. Interfaces' primary purpose is abstraction, decoupling the "what" from the "how".
Source: StackOverflow
Comments
Post a Comment