Tuesday, 5 June 2012

Difference between Abstract class and Interface ?


Difference between Abstract class and Interface ?

Here's a very important concept in Java - to undertand the differences between abstract class and Interface.
its bit tricky,but understanding the usage and implementation gives a clear picture 

Below listed few major difference between these two...

             Abstract Class                                                                     Interface  
If the functionalites(declaration and definition) is same for all classes  then we use Abstract class.

If the declaration is same but the implementation logic is going  differ from  class to class then we use Interface
abstract class can contain abstract and non-abstract methods

every method in interface should be abstract
in abstract class there can be default and instance variables also

by default what ever variables we declare in interface are public static final
An Abstract Class can contain default Implementation
Interface should not contain any implementation. An Interface should contain only definitions but no implementation
When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class
Interface is included in Java to support multiple inheritance.
1) It should contain abstract methods only.
2) It contains only final fields.
3) Subclass may implements any no of interfaces.

Abstract class can provide complete, default code and/or just the details that have to be overridden.

An interface cannot provide any code, just the signature.
Abstract classes are fast.
Interfaces are slow as it requires extra indirection to find corresponding method in the actual class

any class can extend an abstract class..


Only an interface can extend another interface


No comments:

Post a Comment