articles

home / developersection / articles / abstraction and interfaces in java

Abstraction and Interfaces in Java

Abstraction and Interfaces in Java

Ashutosh Kumar Verma 494 19-Mar-2025

Abstraction in Java

Abstraction is a fundamental concept in object-oriented programming (OOP) that hides implementation details and shows only the essential features to the user. It helps to reduce complexity and increase reusability.

In Java, abstraction is achieved by using the below techniques,

  • Abstract Class
  • Interface

 

Abstract Class

  • An abstract class is a class that cannot be instantiated.
  • It can have abstract methods (without implementation) and concrete methods (with implementation).
  • It is used when some methods have the same behavior but some methods need to be implemented differently by subclasses.

Example-

class Main {
   public static void main(String[] args) {
       Vehicle cars = new Car();
       cars.start();
       cars.stop();
   }
}
// Abstract class
abstract class Vehicle {
   // Abstract method (no implementation)
   abstract void start();
   // Concrete method (implemented)
   void stop() {
       System.out.println("Vehicle is stopping...");
   }
}
// Subclass providing implementation for abstract method
class Car extends Vehicle {
   @Override
   void start() {
       System.out.println("Car is starting with a key...");
   }
}

 

Key Points of Abstract Class

  • It can have both abstract and concrete methods.
  • It can have constructors and instance variables.
  • It can have final, static and non-static methods.
  • It cannot be instantiated directly.
  • It can be inherited using extends keyword.

 

Interfaces in Java

Interface in Java is a fully abstract class that contains only abstract methods (before Java 8) and default/static methods (since Java 8). It is used to achieve 100% abstraction and multiple inheritance.

public class Main {
   public static void main(String[] args) {
       Animal myDog = new Dog(); // Upcasting
       myDog.makeSound(); // Output: Dog barks...
   }
}
// Interface
interface Animal {
   void makeSound(); // Abstract method (no implementation)
}
// Implementing the interface
class Dog implements Animal {
   @Override
   public void makeSound() {
       System.out.println("Dog barks...");
   }
}

Key Points of Interface

  • It cannot have instance variables.
  • All methods are public and abstract by default.
  • Supports multiple inheritance (one class can implement multiple interfaces).
  • It cannot have constructors.
  • It can have default and static methods (Java 8+).

 

Summary

  • Abstraction helps to design a clean and structured system by hiding unnecessary details.
  • Abstract classes provide a way to share code while enforcing rules through abstract methods.
    • Interfaces define a contract that multiple classes can follow, allowing flexibility and multiple inheritance.
  • Use abstract classes when you need shared fields or partially implemented methods and use interfaces when you need a strict contract without implementation

 

Also, Read: Understanding Mutable and Immutable Strings in Java, Key Differences and Use Cases


Updated 19-Mar-2025

Hi! This is Ashutosh Kumar Verma. I am a software developer at MindStick Software Pvt Ltd since 2021. I have added some new and interesting features to the MindStick website like a story section, audio section, and merge profile feature on MindStick subdomains, etc. I love coding and I have good knowledge of SQL Database.

Leave Comment

Comments

Liked By