Dinesh Tahiliani wrote:I am asking when to use interface and when to abstract in java.
Well you CAN use abstract classes instead of interfaces. But if you decided to leave *all* of the methods in that top-level class without defining it's body, you should use interfaces.
I meant you should use abstract classes if you need to put some common business logic there. Using interfaces you can mandate it implementers to implement *all* of the methods defined in the interface. Please have a look at this JavaRanch wiki.
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
posted
0
Dinesh Tahiliani wrote:I am asking when to use interface and when to abstract in java.
Use Interface when you want to add optional behaviour in class. Consider Comparable interface that allows a class to make sure that its instances are ordered with respect to other mutually comparable objects. Such interface is called a Mixin. You cannot define Abstract Classes as mixin.
The constant interface pattern is a poor use of interfaces
If you have default behavior, then you must use an abstract class Otherwise, use an interface
An abstract class gives you more flexibility when it comes to evolution. You can add new behavior without breaking clients.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.