Difference Between Abstract Class and Interface

By: | Updated: Feb-19, 2022
The contents of the Difference.guru website, such as text, graphics, images, and other material contained on this site (“Content”) are for informational purposes only. The Content is not intended to be a substitute for professional medical or legal advice. Always seek the advice of your doctor with any questions you may have regarding your medical condition. Never disregard professional advice or delay in seeking it because of something you have read on this website!

Learning to code is a skill that can help you land a job in just about any industry. It’s also a skill that can help you launch your own business. If you are learning Java, you might be familiar with the term abstract class and interface.

In this article, we will discuss what abstract classes and interfaces are, and how you can differentiate them.

Abstract Class Interface
An abstract class cannot be instantiated.

 

Can be instantiated.
Cannot be implemented by a class. Can be implemented by a class.
Abstract classes are typically used to define interfaces. Is part of abstract base classes.
Can obtain both abstract and concrete methods. Can only contain abstract methods.

Difference Between Abstract Class and Interface

An abstract class is a class that is not meant to be instantiated. It is used to define a common set of features or behaviors that can be shared by other classes. In Java, an abstract class can only be used if it is subclassed.

Abstract classes are typically used to define interfaces. They can contain both abstract and concrete methods. Abstract methods are those that must be implemented by a subclass, while concrete methods are those that can be implemented as is.

An interface is a type of abstract class that defines a set of methods that must be implemented by classes that want to implement the interface.

An interface is a class that is meant to be implemented. It is used to define a common set of features or behaviors that can be implemented by other classes. In Java, an interface can be used without being subclassed.

Interfaces are also abstract base classes, but they cannot contain any concrete methods. Interfaces can only contain abstract methods.

An Example of abstract class is Animal, which has the abstract methods eat() and sleep(). An example of an interface is java.util.List, which has the abstract methods add(E) and remove(E). 

We use interfaces when we want to ensure that a class has a certain method, but we don’t care about the implementation. For example, the Collections class has a method called sort(), but does not specify how the sort should be done. This is because the Collections class is an interface, and does not contain any concrete methods. 

We use abstract classes when we want to ensure that a class has a certain method, and we also want to specify the implementation. For example, the java.util.Comparator interface has a method called compare(E, E), which compares two objects. This is because Comparator is an abstract class, which contains the concrete method compare(E, E).

Abstract classes and interfaces can be used to create more modular and maintainable Java code. They can be used to define a common set of features or behaviors that can be shared by other classes. This can help to avoid code duplication and make the code easier to understand and maintain.

(Visited 109 times, 1 visits today)
Did this article help you?
Thank you!
Thank you!
What was wrong?