Difference between abstract class and an interface
This section will help you to find the difference between abstract class and an interface.
This section will help you to find the difference between abstract class and an interface.
Difference between abstract class and an interface
The difference between abstract class and interface is one of the most
popular question in the interview now a days in core java. This section will
help you to find the differences between abstract class and interface in java.
Abstract class is a class for which we cannot create object of it.
Interface is known for fully abstraction in java. Main reason of interface
is to support multiple inheritance in java. Both abstract and interface is a way
to achieve abstraction in java. Now here, we will
find the difference as follows :
- First difference, an abstract class contains both abstract method as
well as non-abstract method(concrete), but interface can contain only
abstract method, no concrete method allow in the interface. Interface
methods are by default abstract and variable are final. Interface
contains only declaration.
- Second difference is that interface is an interface, an interface can
extends another interface while an abstract class is class which cannot
extends multiple classes because java does not support multiple inheritance
but you can implements multiple interface in java.
- Third difference is that abstract class begin with the abstract
keyword and interface is begin with the interface keyword.
- An abstract class should be extending using keyword "extends",
java interface should be implements using keyword
"implements".
- Fourth difference is that interface methods has declaration only not
implementation, the class which implement the interface need to define the
methods of the interface while abstract class can have both abstract and
concrete method. Abstract class contains method with definition.
- Fifth difference is that an abstract class is little bit fast then
interface and interface required extra search for overridden methods.
- There is no any difference between a interface and fully abstract class
i.e. methods are public static final and methods are abstract).
Syntax : How to declare abstract class
abstract class Abstract Demo
{
public int addition(int a,int b){
return(a+b);
}
public abstract int multiplying(int a,int b)}
Syntax : How to declare interface in java
public interface interface-name
{
//variable of interface
//abstract methods
}
Ads