Hi Naga There are two situations when you would need an interface:
1. You create several classes that have a common apparent behavior but they are differently implemented. You wish to let the programmer that uses your classes make abstraction of the way you implemented them.
For example, "container" classes (lists, dynamic arrays ...) define generic interfaces (see, for example, java.util.List) and implementation classes optimized for some particular operations (see for e.g. java.util.ArrayList and java.util.LinkedList). You can write your program using the interface and calling the interface defined methods.
Of course, you will have to choose somewhere the actual implementation you want to use, but it can be a sole "new" statement. The day you will want to used a better suited implementation, you will need to change only the sole "new" statement (in more sophisticated cases, a "factory" allows to generate an implementation instance totally transparent for the code that uses only the interface).
2. You write some code that has to use a class written by someone else that is not available when you write your code. In this case you create an interface, use the interface in your code and ask the other programmer to implement your class.
For e.g. the servlet container "knows" how to use objects of the type "javax.servlet.Servlet". The actual servlets are not available when the container is coded. The actual servlets, written later, will just need to implement the "javax.servlet.Servlet" interface.
In the latest case, you might imagine that there is a typical implementation for some necessary methods. You might wish in this case to create an abstract class that defines the necessary methods (like an interface) but directly implements those for which there is a "default" typical implementation. In this way, the programmer that has to write an actual implementation will spare time by writing only the methods that doesn't use the "default" behavior.
For e.g. javax.servlet.http.HttpServlet is an abstract class already providing "default" implementation for some methods. The HttpServlet programmer doesn't need to implement all the methods, but only those different from the "default" ones.
Another example: AWT (graphic interface) uses "listener" classes whose methods are called on specific user events. For each group of (similar) events, AWT defines an interface that has to be implemented by the actual event listener. For e.g. any mouse event (mouse button pressed, mouse button released ...) triggers one or another of the methods of the listener implementing the interface "java.awt.event.MouseListener".
But if you are interested in only one mouse event (such as the mouse click) and you don't care about the other events, you will still need to write the code for all the methods requested by the interface. In order to simplify the work, the abstract class "MouseAdapter" provide a default implementation for all the methods requested by the interface. Instead of implementing the interface and write all the methods (even the unnecessary ones), you can extend the abstract class and override only the interesting method.
Hope it helps Mihai Le 13/10/2010 13:12, Naga Subrahmanyam a écrit :
In which situation,we can decide the use of Abstract Class or Interface
-- To post to this group, send email to javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en