Many times, I direct maintain seen questions similar why should nosotros role interface inwards Java, if nosotros tin non define whatever concrete methods within interface? Or fifty-fifty to a greater extent than common, What is the existent role of interface inwards Java? I tin empathise beginners bespeak this question, when they merely meet lift of the method within interface together with nada else. It takes fourth dimension to realize existent goodness or actual role of interface or abstraction inwards Java or whatever object oriented programming. One argue of this is lack of sense inwards actually modelling something existent inwards plan using object oriented analysis together with design. In this article, I volition travail to respond this enquiry together with plough over you lot couple of argue to role interface inwards your code. If you lot direct maintain proficient agreement of Object oriented basics e.g. Polymorphism, thus you lot know that it allows you lot to write flexible code. Interface or abstraction are primal to accomplish polymorphism, when a caller role interface for calling a method, he innovate flexibility together with dynamism inwards code, every bit that code volition travel amongst whatever implementation of that interface, non merely the acquaint concrete implementation. You volition never going to instruct this flexibility, if you lot role concrete classes for calling methods, nosotros volition meet this inwards to a greater extent than details inwards side yesteryear side section. Also Programming for Interfaces, is too good recognized together with ane of the key object oriented pattern regulation for coding. Another role of interface inwards Java is that, it opens novel opportunities for other goodies e.g. pattern patterns. Lot's of pattern patterns are heavily relied on interfaces together with Polymorphism e.g. Decorator, Composite, Proxy or Adapter pattern, all implements same interface, every bit at that spot target, together with because they are based on interfaces, they tin live on used interchangeably.
If you lot await closely, you lot volition notice that, nearly of the fourth dimension nosotros role methods from interface. For example, hither nosotros are using get() method from Map interface, you lot could definitely write next code inwards price of HashMap or Hashtable, but thus you lot switched to about other implementation e.g. ConcurrentHashMap, you lot demand to modify your code. Here, nosotros are using Map interface for getting Employee object from Cache, which tin live on whatever valid Map implementation, including HashMap, Hashtable, or ConcurrentHashMap. On similar line, you lot should role interface every bit Type of method arguments together with every bit render type. This makes your code flexible plenty to operate on whatever implementation.
That's all on why you lot should role interface inwards Java. Though you lot tin non define concrete methods, it's best agency to innovate abstraction inwards your design. Real role of interface is to amend code character yesteryear allowing modules to depend upon higher flat of abstraction, which is less probable to modify than lower flat concrete classes, which is to a greater extent than probable to change. Use of interface too opens opportunities for using tried together with tested Java pattern pattern together with immensely helps inwards Unit testing every bit well. Most importantly, role of interface volition plough over you lot dissimilar flat of thinking together with in all probability it's ane measuring inwards mastering object oriented analysis together with design.
Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming inwards Java
Java - Object Oriented Programming [For Absolute Beginners]
Use Interface for Flexibility together with Polymorphism
As I said, polymorphism is powerful together with provides flexibility together with dynamic behavior to your code. In club to convey role of Polymorphism, you lot tin either role interface or abstract cast inwards Java. Interface provides highest score of abstraction. For event next code, which is based on interfaces, together with an event of programming to interface than implementation principle, is flexible plenty to travel amongst whatever Map implementation inwards Java.Mapcache = CacheFactory.getCach(); Employee emp = cache.get("EmployeeID"); // this describe depends upon interface Map, rather than concrete cast e.g. HashMap
If you lot await closely, you lot volition notice that, nearly of the fourth dimension nosotros role methods from interface. For example, hither nosotros are using get() method from Map interface, you lot could definitely write next code inwards price of HashMap or Hashtable, but thus you lot switched to about other implementation e.g. ConcurrentHashMap, you lot demand to modify your code. Here, nosotros are using Map interface for getting Employee object from Cache, which tin live on whatever valid Map implementation, including HashMap, Hashtable, or ConcurrentHashMap. On similar line, you lot should role interface every bit Type of method arguments together with every bit render type. This makes your code flexible plenty to operate on whatever implementation.
Use Interface inwards Java to arrange Design patterns
If you lot are familiar of famous GOF pattern patterns than you lot know that nearly of the behavioural pattern role Polymorphism for dynamic behaviour, e.g. Decorator pattern, implements same interface every bit the object, which it decorates. If you lot don't role interface, it would live on impossible to contain pattern patterns e.g. if your method convey concrete type, rather than abstract type, thus you lot tin non transcend anything else to them. You non exclusively lose flexibility but too runtime behavior enrichment. Similarly, Composite pattern too uses same interface every bit object it contains, together with this allows caller to bargain amongst a normal object or composite object inwards same way, had they don't role interface, it wasn't possible. Proxy together with Adapter pattern pattern too relies on higher flat abstraction e.g. interface or abstract class, to render added behaviour. In short, interface is non merely a proficient coding practice, it too opened upward path to contain other best practices inwards your code.Use Interface to assist Unit Testing
Testing is a big describe of piece of occupation organisation for whatever Project. Project Managers, Technical Pb together with developers volition e'er prefer a code which tin live on unit of measurement tested easily inwards isolation amongst other moving parts. When nosotros role interfaces inwards our code, our method origin depending upon interfaces than implementation, which makes unit of measurement testing extremely easy. For example, a code which is using Database tin non live on tested inwards isolation, until database is available, but if same code is using interface to interact amongst database layer (DAO pattern pattern), you lot tin e'er render a dummy or mock object to mimic database calls. By using interface inwards your code, you lot instruct flexibility everywhere.That's all on why you lot should role interface inwards Java. Though you lot tin non define concrete methods, it's best agency to innovate abstraction inwards your design. Real role of interface is to amend code character yesteryear allowing modules to depend upon higher flat of abstraction, which is less probable to modify than lower flat concrete classes, which is to a greater extent than probable to change. Use of interface too opens opportunities for using tried together with tested Java pattern pattern together with immensely helps inwards Unit testing every bit well. Most importantly, role of interface volition plough over you lot dissimilar flat of thinking together with in all probability it's ane measuring inwards mastering object oriented analysis together with design.
Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming inwards Java
Java - Object Oriented Programming [For Absolute Beginners]