Difference between Abstract Class and Interface?

Below are the difference between Abstract class and an Interface: 

Point of Distinction Abstract Class Interface
Multiple Inheritance No. A class can extend only one abstract class. A child class can only extend a single class (abstract or concrete). Yes. An interface can extend or a class can implement multiple other interfaces.
Types of Methods An abstract class can have abstract and non-abstract method (default and static methods). All Methods of an interface are abstract. In another words Abstract classes can have method stubs (methods without a body) and defined methods, whereas interfaces can only have methods stubs.
Types of Variables Abstract class can have instance, final, non-final, static and non-static variables. Interface has only static and final variables.
Visibility An abstract class can have any visibility: public, private or protected. An Interface visibility must be public or none.
Constructor An abstract class can contain constructors. An Interface cannot contain constructors.
Speed Abstract class is faster than Interface. Interface are slower as it requires extra indirection to find corresponding method in the actual class.
Static Members Only complete members of an abstract class can be static. Member of an interface can not be Static.
Keyword used to extend For abstract we use extends keyword. For Interface we use Implements keyword.
Keyword used to declare Abstract Keyword. Interface Keyword.
Implementation Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
Code Visibility An abstract class can provide complete, default code and/or just the details that have to be overridden An interface cannot provide any code at all, just the signature
Add new method If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method
Extend/implement A class can extend only one abstract class. A class can implement several interfaces.
Example Check below code Check below code

Example

package com.javademo;

import java.io.*;

interface Shape
{
	// Abstract methods inside Interface
	void draw();
	double area();
}

class Rectangle implements Shape 
{
	int length, width;
	
	// Abstract Method can have constructor
	Rectangle(int length, int width)
	{
		this.length = length;
		this.width = width;
	}
  
	public void draw() 
	{
		System.out.println("Rectangle has been drawn "); 
	}

	public double area() 
	{
		return (double)(length * width);
	}
} 

class Circle implements Shape 
{
	double pi = 3.14;
	int radius;
  
	//constructor
	Circle(int radius)
	{  
		this.radius = radius;
	}
  
	public void draw() 
	{
		System.out.println("Circle has been drawn"); 
	}
  
	public double area() 
	{
		return (double)((pi*radius*radius)/2);
	}
}
package com.javademo;

class CI 
{
	 public static void main (String[] args) 
	 {
		 // Creating the Object of Rectangle class using shape interface reference.
		 Shape rect = new Rectangle(10,20);
		 System.out.println("Area of Rectangle: " + rect.area());
 
		 // Creating the Objects of circle class using Shape interface reference.
		 Shape circle = new Circle(5);
		 System.out.println("Area of Circle: " + circle.area());
	 }
}

Comments