Java Abstract & Interface

Question : What is the difference between an abstract class and an interface and when should you use them?
Answer : In design, you want the base class to present only an interface for its derived classes. This means, you donΓÇÖt want
anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which
gives you polymorphic behaviour), so that its interface can be used. This is accomplished by making that class
abstract using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents
The interface keyword takes this concept of an abstract class a step further by preventing any method or function
implementation at all. You can only declare a method or function but not provide the implementation. The class,
which is implementing the interface, should provide the actual implementation. The interface is a very useful and
commonly used aspect in OO design, as it provides the separation of interface and implementation and
enables you to:
– Capture similarities among unrelated classes without artificially forcing a class relationship.
– Declare methods that one or more classes are expected to implement.
– Reveal an object’s programming interface without revealing its actual implementation.
– Model multiple interface inheritance in Java, which provides some of the benefits of full on multiple
inheritances, a feature that some object-oriented languages support that allow a class to have more than one

java abstract and interface

Question : When to use an abstract class?:
Answer : In case where you want to use implementation inheritance then it is usually
provided by an abstract base class. Abstract classes are excellent candidates inside of application frameworks.
Abstract classes let you define some default behaviour and force subclasses to provide any specific behaviour.

Question : When to use an interface?:
Answer : For polymorphic interface inheritance, where the client wants to only deal with a type
and does not care about the actual implementation use interfaces. If you need to change your design frequently,
you should prefer using interface to abstract. CO Coding to an interface reduces coupling and interface
inheritance can achieve code reuse with the help of object composition. Another justification for using interfaces
is that they solve the ΓÇÿdiamond problemΓÇÖ of traditional multiple inheritance as shown in the figure. Java does not
support multiple inheritances. Java only supports multiple interface inheritance. Interface will solve all the
ambiguities caused by this ΓÇÿdiamond problemΓÇÖ.

Question : Why there are some interfaces with no defined methods (i.e. marker interfaces) in Java?
Answer : The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes
implementing the interfaces with no defined methods need to be treated differently. Example Serializable , Cloneable etc.

Leave a Reply