In real world interface allows two disparate/ different objects to communicate with each other. For instance a human is a very different object when compared to television object. But they can still use the remote control interface to communicate with each other. Same holds true for interfaces in software, it defines a communication vocabulary between objects. Interfaces have empty methods and we can not create an object of interface.
Figure: - Television interface
The other important thing is that interface brings in lot of uniformity in the project. They define common way of communication. Let’s consider a situation where you have multiple developers working on customer and supplier classes. You want that every developer should define the method name as ‘Update’ when these classes add the data to database. Developers are human’s so some would name it as update and some as add. You can tackle this situation by defining an interface with a method called as update. Now all the classes inherit from the same interface thus making update method constant through out the classes. You can also see how the client code methods are looking consistent in naming convention thus enforcing consistent communication interfaces across all classes.
Figure: - Interface fundamentals
Also see the OOPS fundamental video on polymorphism and its types as follows: -
No comments:
Post a Comment