×

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s have a look below.

Abstract ClassInterface
An abstract class can have abstract, non-abstract, default, and static methods.An interface can have only abstract methods.
It extend another java class and implement multiple java intfaces.The interface extends another interface only.
It can have class members like public, private, protected, etc. Example:   abstract class Student {      private String name;      public void setName(String n) { name=n;} public String  getName()   {       return(name);    } }The interface is public by default, and we cannot declare it private or protected.   Interface Calculate {      double veg=25.50; intnon_veg= 55; int add(veg, non_veg) }
It may contain non-final variables.Variables in the interface are declared final by default.
It can have a final, non-final, static, and non-static variable.It has only static and final variables, but the function is non-static by default. We can’t declare a function as static.
It provides the implementation of the interface.It cannot provide the implementation of the Abstract class.
It can be extended using the keyword “extends.”It can be implemented using the keyword “implements.”
We can define a constructor in the abstract class if we can’t define any constructor, the compiler will create a constructor by default.We can’t create a constructor inside an interface, and not even created by the compiler by default.
Abstratc class achives partial abstraction.Interface achieves fully abstraction.

Example to illustrate the working of Abstract class and Interfaces in java.

 //Creating an interface that has 4 DMAS methods
interfaceinterfaceDemo
{
//methods are by default, public and abstract
void add();
void sub();
voidmul();
void div();
}
//Creating an abstract class that provides the implementation of one method of interfaceDemo
abstractclass Demo implementsinterfaceDemo
{
publicvoid add()
{
System.out.println("Add method");
}
}
//Creating a subclass of abstract class
classDmasextends Demo{
publicvoid sub()
{
System.out.println("Subtract Method");
}
publicvoidmul()
{
System.out.println("Multiplication Method");
}
publicvoid div()
{
System.out.println("Division Method");
}
}
//Creating a class that calls the methods of interfaceDemo
publicclassAbstractInterfaceDemo {
publicstaticvoid main(String args[]){
interfaceDemo a=newDmas();
a.add();
a.sub();
a.mul();
a.div();
}
} 

Related Topics

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.