Interface Vs Abstract class Java

Abstract class vs Interface 
Type of methods: Interface can have only abstract methods. An abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables.
Implementation: Abstract class can provide the implementation of the interface. Interface can’t provide the implementation of an abstract class.
Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract class can be extended using the keyword “extends”.
Multiple implementations: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

What is difference between functional interface and interface?

Any interface with a SAM(Single Abstract Method) is a functional interface, and its implementation may be treated as lambda expressions.

Interface can have any number of abstract methods. 

A functional interface is an interface annotated with @FunctionalInterface annotation and contains only one abstract method, 
but the interface can have multiple default methods. 
Because Runnable is a functional interface so has only one abstract method run(), we can create a Thread object using a lambda expression.
Thread t = new Thread(()-> System.out.println("Runnable Interface"));

Java Predefined-Functional Interfaces
Java provides predefined functional interfaces to deal with functional programming by using lambda and method references.

You can also define your own custom functional interface. Following is the list of functional interface which are placed in java.util.function package.
Supplier, Consumer , Function Predicate etc.

Interface :
Interface is a group of related methods with empty bodies(Signatures). It is not a class. One class may implements multiple interfaces thus helps to achieve multiple inheritance. All the methods in interface are by default abstract with out any abstract key word.
Examples for interface :
interface vehicle {
void startVehicle(Sting commandToStart);
void stopVehicle(String commandToStop);
void speedUp(int speed);
to implement the above interface we can have mutiple classes like Bycycle, MotorBike, Car etc.
class MotorB{

Abstract Class :
A class declared as abstract - It may or may not contain abstract methods. It can't  be instantiated but can be subclassed. Means an abstract class can only be subclassed which mean it can be inherited from. In simple words other classes can inherit an abstract class but they can't initiate the abstract class. One class can extend only one abstract class.
Abstract method :

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:Abstract classes are classes that contain one or more abstract methods.

public abstract class vehicle{

//Non abstract method
void int speedUp(int speed){

spedd = speed + 10 ;


abstract void stopVehicle(String commandToStop);


public class thunderBird extends vehicle{

void stopVehicle(String commandToStop)




So in the above abstract class vehicle there are two methods one is speedUp which is non-abstract method and one abstract method stopVehicle . So the classes that extend this abstract class must define the abstract methods declared in abstract class other wise the class itself must declared as abstract class again.

An abstract method is a method that is declared, but contains no implementation.
Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
An Abstract class without any implementation just looks like an Interface.

Abstract class can implement interface ..

abstract class jumbo implements vehicle {
  void startVehicle(Sting commandToStart){

start ;

void stopVehicle(String commandToStop){

stop ;

void speedUp(int speed){

speed = speed + 20;


Refer some more pages on the same topic here [1][2][3][4]

Technorati Tags:

4 comments to "Interface Vs Abstract class Java"

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...


Powered by WidgetsForFree