DEVFYI - Developer Resource - FYI

Why are the interfaces more flexible than abstract classes?

Java Interview Questions and Answers (part 3)


(Continued from previous question...)

403. Why are the interfaces more flexible than abstract classes?

--An interface-defined type can be implemented by any class in a class hierarchy and can be extended by another interface. In contrast, an abstract-class-defined type can be implemented only by classes that subclass the abstract class.
--An interface-defined type can be used well in polymorphism. The so-called interface type vs. implementation types.
--Abstract classes evolve more easily than interfaces. If you add a new concrete method to an abstract class, the hierarchy system is still working. If you add a method to an interface, the classes that rely on the interface will break when recompiled.
--Generally, use interfaces for flexibility; use abstract classes for ease of evolution (like expanding class functionality).

(Continued on next question...)

Other Interview Questions