Hi folks, When do we use abstract class and when do we use interface? What are the advantages of using Abstract class? Can i use an abstract class with only method declarations instead of interface.. Bye Vinay
Vinay, Use an abstract class when you want to implement some but not all of its behavior, and especially if your intent is that subclasses will define only a small subset of the behavior. If you just want to make a specification with no implementation, it's better to use an interface. Concrete reason: When you use an abstract class, subclasses can't extend any other class, which puts a real restriction on architecture solutions. Philosophical (and more important) reason: It's good design for subsystems to interact by programming to each other's interfaces, and the use of a Java <code>interface</code> enforces this choice. It reduces coupling between subsystems. jply
Another option and one that is used frequently in the jdk api is to define both an interface and an abstract implementation of it ... so you get the best of both worlds ... you have both a pure interface and a class that implements a lot of the base functionality in a sensible default manner. Code that makes use of your objects will use the interface, but you can create subclasses by extending the abstract class without having to redefine all the base functionality in each subclass.