Saturday, July 28, 2012

Difference between abstract class and interface


1. Abstract class is a class which contains one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods.

2. Abstract class is a Class prefix with a abstract keyword followed by Class definition. Interacace is a Interface which starts with interface keyword.

3. Abstract class contains one or more abstract methods. Whereas Interface contains all abstract methods and final declarations.

4. Abstract class contains the method definition of the some methods. But Interface contains only method declaration, no definition provided.

5. Abstract classes are useful in a situation that some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented we can use this scenario

When to Use
If the functionalites(declaration and definition) is same for all classes in the project and that is mandatory that time we use Abstract class.
If the declaration is same but the implementation logic is going  differ in class to class that time we use Interface

No comments:

Post a Comment