Java is a widely used Object-Oriented Programming language, which is used to build various software applications such as enterprise applications, Android development, web applications, and big data technologies, cloud computing, IoT and Game development. This programming language was introduced by James Gosling and his team at Sun Microsystems.
They developed it with the purpose of developing a platform-independent language for small and embedded electronic devices such as set-top boxes, televisions, remote controllers, and VCRs.
In this article, we’ll cover the topics of abstract class and interference in Java. Both are mechanisms used to achieve abstraction. An abstraction is a process of hiding complex essential implementation details and showing users only the required information for using an object rather than knowing its internal mechanism.
What are Abstract Class and Interface in Java?
An abstract class is a class declared using the abstract keyword that is used when you want a base class that has some common methods already implemented, but also requires subclasses to provide their own specific implementations for other methods like applying the brake for a vehicle but break mechanism is car is different from motorcycle.
Example
abstract class TpointTech {
abstract void printInfo();
}
// Abstraction performed using extend
class Employee extends TpointTech {
void printInfo()
{
String name = “Abhay”;
int age = 21;
float salary = 22.2F;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
// Base class
class Base {
public static void main(String args[])
{
TpointTech emp = new Employee();
emp.printInfo();
}
}Output
Abhay
21
22.2Explanation: In the above program, TpointTech is declared as an abstract class and it contain abstract method printInfo() to enforce a common method structure in subclasses while allowing each subclass to provide its own implementation.
An interface is a template of a class that contains abstract methods or constants that define a set of behaviors that a class must agree to implement. It supports multiple inheritance, where it can inherit behavior from multiple sources. In Java, an interface is implemented using the implements keyword.
An interface can be explained with a real-life example such as payment processing in e-commerce where paymentprocess() is an interface for making payments and classes like CreditCardPaymentProcess, DebitCardPaymentProcess and UPI would all implement this interface.
Given below is an example of performing an interface in a Java program.
Example
class UPI implements Payment {
public void pay() {
System.out.println(“Payment done via UPI”);
}
}
public class Main {
public static void main(String[] args) {
Payment p = new UPI();
p.pay();
}
}Difference between Abstract Class and Interface
Given below is the table describing the key difference between abstract class and interface in Java.
| Features | Abstract Class | Interface |
| Method Implementation Multiple Inheritance Variables Constructors Access Modifiers Performance Use Case Keyword Java 8 features | It can have both abstract and concrete methods. It does not support multiple inheritance. It can have instance variables. It can have constructors. It include private, protected or public. It is slightly faster due to method calls. It is used when classes share common state and behavior. Abstract class is implemented with abstract keyword. It includes normal methods. | It can have only abstract methods. It support multiple inheritance. Variables in interface are implicitly public, static, and final. It cannot have constructors. These methods are public by default. It include interface method invocation which is slightly slower. It is used when multiple classes need to follow the same contract. Interface is implemented through interface keyword. It includes default and static methods. |
When to Use Abstract Class?
Given below is the list of scenarios where you can use abstract class.
- Sharing Common Code Among Subclasses: For example animal abstract class has a makeSound() method that is specific to each animal.
- Classes are closely related: Startengine() in transport machine but mechanisms of start engine are different for car, bike, aeroplane and ship.
- You need constructors: To initialize the common state shared by all concrete subclasses and to enforce class invariants.
- You want to provide base functionality: Defining common behavior once and allowing multiple classes to reuse or extend it.
When to Use Interface?
Given below is the list of scenarios where you can use interface:
- You want multiple inheritance: Example include a Duck that can walk and swim. In this you define two interfaces walkable, swimmable and a class Duck that implements both.
- To define a Contract or API: Different parts of a system need to interact based on a standardized set of methods with their internal implementation remains hidden. The Java Database Connectivity (JDBC) API is a classic example provide this featues.
- Classes are not closely related: It refers to a situation where multiple classes do not share a common parent behavior but still need to follow the same set of rules. They belong to different categories or hierarchies.
- You want to achieve loose coupling: It refers to designing components so that they have minimal dependencies on each other’s concrete implementations.
Final Thought
Both abstract classes and interface are important concepts in Java programming. Abstract classes are used when you need a common base for related classes that share some behavior but need unique implementations for specific methods. And, interfaces are used to define contracts for behavior, achieve abstraction, enable multiple inheritance of type and promote loose coupling.

