Friday, 21 February 2014

Abstract Class versus Interface

1. Interfaces support multiple inheritance, where as abstract classes are not.

2. All the fields declared in interface are static final, where as you can declare static, non static, final, non-final variables in abstract class. There is no restriction in abstract classes.

3. All the methods declared in interface are abstract by default. It is not necessary for an abstract class to have abstract methods.

4. Abstract class can have concrete methods, where as interface don't. (From Java8 onwards we can provide default behaviour to methods in interfaces)

5. Abstract class can have constructor, where as interface not.

6. Abstract classes are used to share the common behavior. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

7. An abstract class can implement the interface, it doesn't mean that it provides implementation for all the methods in the interface

Some Points to Remember
1. If I can simulate Abstract class as Interface, why java provides Interface?
because you can implement multiple interfaces, but can't extend multiple abstract classes. To support multiple inheritance, interfaces are necessary.


Abstract methods & Abstract classes                                                 Garbage Collection                                                 Home

No comments:

Post a Comment