×

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

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.