Abstraction in Java: 2 Essential Concepts to Master – Abstract Classes and Interfaces

Abstraction in Java

  • Abstraction is one of the core concepts in Object-Oriented Programming (OOP), and it plays a crucial role in simplifying complex systems by hiding the implementation details and exposing only the essential features.
  • In Java, abstraction is achieved through abstract classes and interfaces.

Definition of Abstraction:

  • Abstraction refers to the process of hiding the implementation details of an object and only exposing the necessary information.
  • This allows the user to interact with the object without needing to understand its internal workings.
  • Abstraction allows focusing on what an object does, rather than how it does it.

Types of Abstraction in Java:

  1. Abstract Classes
     
  2. Interfaces
Abstraction in Java credit by https://www.simplilearn.com/ice9/free_resources_article_thumb/Ways_of_data_abstraction-Abstract_Class_in_Java.png
Abstraction in Java

1. Abstract Class in Java

  • An abstract class in Java is a class that cannot be instantiated (i.e., you cannot create objects of an abstract class directly). It is meant to be extended by other classes.
  • Abstract classes can have both abstract methods (methods without a body) and concrete methods (methods with a body).
  • Abstract methods are declared without an implementation. Subclasses that extend the abstract class must provide an implementation for these methods unless the subclass is also abstract.
  • Concrete methods are regular methods that have an implementation.

Syntax of an Abstract Class:

abstract class Animal {
// Abstract method (does not have a body)
abstract void sound();

// Regular method (has a body)
void sleep() {
System.out.println(“This animal is sleeping”);
}
}

Example of Using Abstract Class:

abstract class Animal {
// Abstract method (does not have a body)
abstract void sound();

// Regular method (has a body)
void sleep() {
System.out.println(“This animal is sleeping”);
}
}

class Dog extends Animal {
// Providing implementation for abstract method
@Override
void sound() {
System.out.println(“Bark”);
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog(); // Creating an object of the subclass
dog.sound(); // Output: Bark
dog.sleep(); // Output: This animal is sleeping
}
}

Explanation:

  • The sound() method in the Animal class is abstract, so it doesn’t have an implementation.
  • The Dog class extends Animal and provides a concrete implementation of the sound() method.
  • The sleep() method is a regular (concrete) method and is inherited as is.

 

2. Interface in Java

An interface is similar to an abstract class, but it can only contain abstract methods (in Java versions before Java 8) and constants. In Java 8 and later, interfaces can also have default methods (with a body) and static methods.

  • Key points about interfaces:
    • All methods in an interface are implicitly abstract (unless defined as default or static).
    • A class can implement multiple interfaces, allowing multiple inheritance of behavior.
    • Interfaces are used to define a contract that implementing classes must adhere to.

Syntax of an Interface:

interface Animal {
// Abstract method (does not have a body)
void sound();

// Default method (with a body)
default void sleep() {
System.out.println(“This animal is sleeping”);
}
}

Example of Using Interface:

interface Animal {
// Abstract method (does not have a body)
void sound();

// Default method (has a body)
default void sleep() {
System.out.println(“This animal is sleeping”);
}
}

class Dog implements Animal {
// Providing implementation for abstract method
@Override
public void sound() {
System.out.println(“Bark”);
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog(); // Creating an object of the implementing class
dog.sound(); // Output: Bark
dog.sleep(); // Output: This animal is sleeping
}
}

Explanation:

  • The Animal interface contains an abstract sound() method and a default sleep() method.
  • The Dog class implements the Animal interface and provides an implementation for the sound() method.
  • The sleep() method is inherited from the Animal interface and does not need to be overridden unless required.

 

Key Differences Between Abstract Class and Interface:

FeatureAbstract ClassInterface
PurposeUsed to represent a class that shares common behavior but should not be instantiated directly.Used to represent a contract that classes can implement.
MethodsCan have both abstract (without implementation) and concrete (with implementation) methods.Can only have abstract methods (unless using Java 8’s default methods or static methods).
ConstructorsCan have constructors.Cannot have constructors.
Multiple InheritanceA class can inherit from only one abstract class.A class can implement multiple interfaces.
Access ModifiersCan have any access modifier (private, protected, public).All methods in an interface are implicitly public.
FieldsCan have instance variables (fields).Can only have constants (implicitly public static final).
Keyword UsedUse the abstract keyword.Use the interface keyword.
Default MethodsCannot have default methods (before Java 8).Can have default methods (from Java 8 onwards).

 

When to Use Abstraction in Java:

  • Use an abstract class when you have a common base class with shared behavior, but you want to leave certain methods unimplemented for subclasses to define.
  • Use an interface when you want to define a contract that can be implemented by multiple classes, possibly from different inheritance hierarchies.

Leave a Comment