Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Object-Oriented Programming

What is Abstraction in Java?

Abstraction in object-oriented programming hides the internal implementation while exposing only the essential details. In Java, abstraction is achieved using abstract classes and interfaces.

An interface provides 100% abstraction, while an abstract class allows partial or full abstraction. Abstraction simplifies complex systems by emphasizing what an object does rather than how it works. This concept is also known as detail hiding.

Example

  • When we drive a car, we simply press the brake to slow it down without knowing the internal mechanics. Similarly, to start the car, we just press the start button, and the engine starts. The internal workings of these actions are hidden from us. This abstraction ensures that users can operate the car without understanding its underlying processes.

  • On the ATM screen, banks highlight the services they offer without revealing the internal implementation. These details are abstracted from users, providing greater flexibility and making the system easy to use.

In Java, we can achieve abstraction by using an abstract class and an interface

What is an Abstract class?

A class that is declared with the abstract Keyword is known as an abstract class. Or we can say that a class in which we can develop an abstract method is called an abstract class. We can’t create an object of the abstract class. It should always have the subclass, and that subclass should override all the inherited methods.

Abstract method: The method that is declared with the abstract keyword without a definition is called an abstract method.

abstract void masterBackend();

Concrete method: The method that is declared with a definition is known as the concrete method.

void masterBackend()
{
	// do somthing
}

Concrete class: A class that contains only concrete methods is known as a concrete class.

public class masterBackend
{
		void masterBackend()
		{
			// do somthing
		}
	}

Abstract classes contain both types of methods, abstract methods as well as concrete methods. That’s why we can achieve partial 100% abstraction with an abstract class.

Here are some points to note about abstract classes:

  1. We can’t create an object of the abstract class.

  2. Abstract class & abstract method should be declared with the abstract keyword.

  3. When we develop the method as abstract, the class must also be abstract.

  4. In abstract, we can develop any kind of methods

  • We can develop only concrete methods.

  • We can develop only abstract methods.

  • We can develop both types of methods.

Syntax

MasterBackend.java

abstract class MasterBackend {
    
    int variable1;
    String variable2;

   
    public ClassName(int variable1, String variable2) {
        this.variable1 = variable1;
        this.variable2 = variable2;
    }

    // Abstract method (must be implemented in child classes)
    abstract void abstractMethod();

    // Concrete (non-abstract) method
    void concreteMethod() {
        System.out.println("This is a concrete method in an abstract class.");
    }
}

Example

// Abstract class
abstract class Animal {
    // Abstract method (no implementation)
    abstract void makeSound();

    // Concrete method (has implementation)
    void sleep() {
        System.out.println("Sleeping...");
    }
}

// Subclass (inherits from Animal and provides implementation for makeSound)
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks!");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();  // Create an object of Dog
        myDog.makeSound();  // Output: Dog barks!
        myDog.sleep();  // Output: Sleeping...
    }
}

Output

Dog barks!  
Sleeping...  

In the above example, we define an abstract class called Animal, which serves as a blueprint for all animal types. It contains an abstract method makeSound(), which has no implementation and must be defined in any subclass. Additionally, it has a concrete method sleep(), which prints "Sleeping..." when called.

The Dog class extends Animal and provides an implementation for makeSound(), printing "Dog barks!" when invoked.

In the Main class, we created an instance of Dog and called both makeSound() and sleep(). Since makeSound() it is implemented in Dog, it prints "Dog barks!" and since sleep() it is already defined in Animal, it prints "Sleeping...".

Advantages

  • Abstraction (detail hiding)

  • Standardization

An interface provides 100% abstraction, while an abstract class allows partial or full abstraction. Abstraction simplifies complex systems by emphasizing what an object does rather than how it works. This concept is also known as detail hiding.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board