Ebay Interview Question
Front-end Software EngineersHow about this, we use interface when we want to work with generic function. In this case, encapsulating the object properties (e.g., comparable, serializable, etc.) is more than enough. We often use abstract to encapsulate the behaviors to achieve the polymorphism. Working with polymorphic object, we are touching the semantic of the function where the concrete implementation/behavior is encapsulated. Don't forget that we can not use interface to create polymorphic objects.
Even interfaces can be used to perform dynamic polymorphism
Eg: Interface Shape ()
{
String getShapeName();
}
class Square implements Shape {
String getShapeName () {
return "square";
}
}
Also
class Circle implements Shape
{
....//implemnents getShape () as well.......
}
During run time:
Shape shapeObject = getShapeObject();
// the object assigned is decided dynamically..
Shape getShapeObject()
{
if Tuesday return Square;
else return Circle;
}
Abstract class is a normal class just that we cannot instantiate an instance of it. It has to be subclassed to be instantiated.
And Abstract class is best suited when we have common functions or data to be shared among its children classes.
Interface is a contract with the actual implementation and the usage.
It defines the clear boundary of usage without worrying of how or who is implementing it.
In Abstract class, we can have implementation of a few functions to provide the default functionality while having at least 1 pure virtual function.
- Anonymous June 13, 2011I interface we cannot have implementation of any function.