Tuesday, March 26, 2013

Interface Vs Abstract class

 Difference between Interface and Abstract class                   

                       The tricky and most important question in core java interview is "Difference between Interface and Abstract class".
                       Now let's see difference between Interface and Abstract class in java

1. You cannot create non abstract methods in Interface because every method in iterface by deafault abstract method, but you can create non abstract methods in Abstract class.

2.Interface is suited for Type declaration and abstract class  is suited for code reuse and perspective.

3.When  you add a new method in exiting interface it breaks all its implementation and you need to provide implementation in all clients which is not good,but by using abstract class you can provide deafault implementation in super class.

4.Variable declaration in java interface is by default final.An abstract class may contain non final variables.

5.Methods of interface are public and abstract by default.Methods of abstract class can have  the flavour of class members like private,protected,etc.

6.An interface can extend another interface only but java abstract class can extend another java class and implements more than one interfaces.

7.when compare interface with java abstract class, interface are slow and it requires extra indirection.

8. Interface cannot contain the constructor by default or by programmer but abstract class contains constructor by default or by providing programmer.

No comments:

Post a Comment