Friday, 2 February 2018

Abstract class Vs Interface in java

Abstract class and interface differ based on following parameters:


Methods

An abstract class can have both abstract methods and concrete methods.
An interface has all the methods as abstract. Methods in interface is 100% abstract.

Keyword used to declare

To declare abstract class use the keyword ‘abstract’. 
Example abstract class dummyClass{//abstract methods, methods}

To declare interface use the word ‘interface’. 
Example interface dummyInterface{//abstract method (s)}

Method declaration

In abstract class, to declare any method as abstract use of abstract keyword is mandatory.
In interface, to declare methods as abstract use of abstract key is optional. Methods in interface is by default abstract.

Inheritance Supported

Being a class, an abstract class cannot inherit multiple classes. It can only inherit one class.
An Interface can extend (or inherit) multiple interfaces.

What can be extended?

An abstract class can extend either normal class or abstract class.
Interface can extend only interface but not a class.

Variable type & Access modifier

An abstract class can have static, non-static, final or non-final variable with any access modifier.
An interface can only have public static final variable. In interface variable is constant.
 

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.