Goldman Sachs Interview Question
Software Engineer / DevelopersInterface vs. abstract class
Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent candidates inside of application frameworks.
Abstract classes let you define some behaviors; they force your subclasses to provide others. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract base class can declare abstract shutdown and startup methods.
Use Abstract class when you are designing a family inheritance.
eg.,
Animal
| |
DOG CAT
Use interface to provide additional behavior for a class... eg., Swimmable interface implemented by Fish and by Swimmer.
Fish and Swimmer do not have anything in common except that they both can swim.
download.oracle.com/javase/tutorial/java/IandI/abstract.html
abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way.
Existing class hierarchy can be easily retrofitted to add new behavior using interfaces. Interfaces allows to define mixed behaviors. Interfaces allows constructions of non-hierarchial frameworks.
Abstract classes are easy to evolve. Adding new behavior to abstract classes is simpler than interfaces.
Prefer abstract classes if simplicity is more important than flexibility.
It is worth reading Item 18 in Effective Java to understand why to prefer interfaces over abstract classes.
Interface provides method signature and not the body. Its up to the developers to
- Shri February 19, 2010provide body for these methods once they implement it.
Abstract provides both methods with and without body. Adding new methods in abstract class in middle of development life cycle won't break existing sub classes unless these methods are declared as mustoverride. If there are frequent addition of new methodsproperties and so on. one should use abstract..
Adding new methods to the interface will force users to go through each and
every sub class that implemented that interface and call these methods.
So its always preferred to use abstract over interface because of this limitation.
Whereas on the other hand interface can fill gap of multiple inheritance. One can
implement from more than one interface. Not same with Abstract. One can inherit
from only one abstract class.
If you want to inherit from class that can provide reusable code then use abstract class. For instance connecting to the database or base class for grid or treeview.
If you implement your subclass from an interface then in each subclass you will have to write your own code to fill body of these methods.
Hope this helps.