Amazon Interview Question
Software Engineer InternsCountry: United States
Interview Type: In-Person
Wouldn't it be more advantageous to make an interface that is something along the lines of IStorable which describes the methods each object might need to interact with the shelf/other objects? Then the shelf would hold IStorable objects which decouples what type of object can be stored rather than having to inherit from Base.
This seems like a question to check composition and inheritance concepts. Design should be flexible enough to accomodate future changes. Keeping this in mind we can design a base class which will base for any element to be stored on the shelf bringing in inheritence. Shelf is a separate class having fields to identify allowable types that can be stored on the shelf. Shelf class can contain any collections to allow adding and removing objects from it.
- Abh September 07, 2013abstract class Base{
.........
}
class Book extends Base implements PrintedMediumInt{
....................
}
Class DVD extends Base implements ElectronicMedia{
....................
}
class Shelf{
ArrayList <Base> collection;
}
Inheritence is used for object creation and shelf uses composition. In addtion object creation can be done via abstract factory. Further extension for CD/DVD/Blueray can be achieved using decorators.