VMWare Inc Interview Question
Software Engineer / DevelopersTeam: Cloud Resource Management
Country: United States
Interview Type: Phone Interview
It's not just for factory methods, though surely those could return interfaces at times. It's for any situation where you wish to present a certain view of a certain category of classes. For instance, consider a KeyListener interface. This interface can be implemented by any class wishing to process keystrokes. It presents a view of such classes that only involves the methods that are related to processing keys. That is, when you look at a KeyListener object, all you know is that this object can call keyPressed(), keyReleased(), etc. -- in short, you're just seeing that one aspect of the object that is related to keystrokes. All other info has been abstracted away -- you're not seeing all the unrelated functionality that object may have (perhaps its core functionality, the keyboard input being a minor aspect).
Interfaces define a common contract for unrelated classes that can extend some other classes and inherit behavior apart from the behavior they already have inherited from interfaces. When the design decisions are very dynamic and lot of frequent changes are to be accomodated in future, interfaces are more efficient than classes. Even Gang of Four recommends using interfaces.
- Interfaces are used for writing extensible code, when you modify an implementation of an interface, you ensure that this change will not effect the system because only the implementation has changed but not the contract, so when you really need to modify the implementations without really affecting how the application uses it, you use interfaces.
- And usually it is a good practice to write interfaces because they also help you in Mocking and unit testing of your code.
- It is also a good practice to ensure that you set the implementations of interfaces in your application using setter methods because that ensures more ways to mock your objects and helps in loose coupling.
Interfaces, in general define a contract which the implementing classes should be using. The best use of interfaces can be seen in the Collections API.
e.g The List<E> interface defines methods like get(), set(), add() ... which are generalised operations on List. The implementing classes like ArrayList, Vector give meaning(definitions) to these contracts.
The other advantage of using interfaces are parameterized polymorphism. It gives you the flexibility to bind the "type" of parameters to functions at run time.
Here i'm giving few real usage of/ reason behind Interface
1>Extensibility /flexibility ( for designing point of view)
http:--java.sys-con.com--node--36250
2> We can achieve more polymorphism than using class/abstract polymorphism
www--artima.com--designtechniques--interfaces3.html#getting
3> we can implement inheritance using interface with composition
4> Implementing design patterns and finally Multiple inheritance and no diamond problem
Interfaces help in creating a loosely coupled code. how?
ok here it goes
consider this
- Gagan Suri February 08, 2012