Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
*I voted on the other, this is just another explanation from codeproject
The only difference is that: An abstract class can have instance methods that implement a default behavior.
If the requirement is such that you have to force a default behavior to all your classes. best way to to write that default instance method to an abstract class so that all classes extending this particular abstract class will have that default behavior.
The major difference between abstract class and interface is in case of abstract class we can say that its a partially implemented structure where an interface is fully unimplemented structure. Now taking consideration of other facts like all the variables declared in an interface are public static in nature as well as if we write any class also by default public static. No method can have body part. And in case of abstract class we may or may not put the body part of the methods. If we do not put any body part in the abstract method the it should have public abstract.
Abstract class can have have states (instance variable) while interface cannot have. Abstract class can have some method implemented while interface cannot have. Abstract class can have method overridden by only its subclasses while interface have can have its method implemented by all other classes that implements the interface. More?
I think that this question expects more than syntactical or language dependent differences, and one should tell when to use interface and when to use abstract class.
- Learner September 26, 2012The key notion behind use of interface is adhering to a contract. All the features of an interface are abstract, and it is the implementor's responsibility to implement those.
Abstract class can be used when atleast some functionality is concrete enough to be carried forward via inheritance. Most of the listener classes are abstract classes, which provide native functionality implemented.