Wednesday, 1 February 2017

DIFFERENCE BETWEEN ABSTRACT CLASS AND INTERFACE IN JAVA

Abstract Class
Interface

It is a partial abstract class, it allows us to define both concrete and abstract methods.

It is purely an abstract class, it allows us to define only abstract methods.
It provides reusability and forcibility.

It provides only forcibility.
It should be declared as abstract using abstract keyword, abstract methods also should contain abstract keyword.

Declaring it as abstract is optional, declaring its methods as abstract is also optional. Compiler places abstract keyword at compilation time.

It’s default accessibly modifier is package, can be changed to public.

Its default accessibly modifier is also a package, can be changed to public.

Its members default accessibly modifier is package, can be changed to any of other three accessibility modifiers.

Its members default accessibly modifier is public, cannot be changed.
Its variables are not by default, static and final
Its variables are by default static and final

Static final variables no need to be initialized at the time of creation, but should be initialized in any one of the static blocks.

In interface, variables should be initialized at the time of creation as there is no static blocks.
Inheritance relation is established using “extends” keyword.
Inheritance relation is established using “implements” keyword

Can have inner class
Can have inner class

No comments:

Post a Comment