Pages

Tuesday 26 November 2013

Difference between Abstract class and interface in Java





Abstract class  

Interface

1.  A class become abstract when we use ' abstract ' keyword before the class name.

1. No need of using any abstract keyword but we need to use ' interface ' keyword. 

2. We can use instance variable.

2. We cannot use any instance variable.
3. Can define static/ non-static variable.

3. Variables are by-default static.

4. Can use static/ non-static method

4. Static and non-static both cannot be used.

5. Can use private and protected with the variable 

5.  Cannot use private and protected with the variable.

6. Static and instance both initialization block are allowed. 

6. Both static and instance initialization block are not allowed.

7. Constructor are used to initialize instance variable defined in abstract class. 

7. Cannot define constructor in an interface.

8.Cannot be used for achieving multiple inheritance. 

8. It is used to achieve multiple inheritance in java.

9. Can define final and non-final method but final method should not be abstract. 

9. Cannot define final and non-final method.

10. Can define abstract and non-abstract method in an abstract class. 

10.Cannot define non-abstract method and by-default method are abstract.

11.' extends ' keywords are used to extend abstract class. 

11.' implements ' keyword are used to implement any interface.

12. It extends java.lang.Object class.

12.It cannot extend java.lang.Object class.

13. Can define main method in an abstract class. 

13.Cannot define main method in an interface.

14. Instance ' inner class ' is allowed inside as abstract class. 

14. Instance inner class in not allowed and will be converted into static inner class by compiler.

No comments:

Post a Comment