Loading...

Abstraction in Java

1. Introduction to Abstraction

Abstraction is a process of hiding the implementation details and showing only the essential features of an object. It focuses on what an object does rather than how it does it. Abstraction in Java is achieved through abstract classes and interfaces.


2. Abstract Classes

  • An abstract class is a class that cannot be instantiated and can contain abstract methods (methods without a body) as well as concrete methods (methods with a body).
  • Abstract classes are declared using the abstract keyword.

Syntax:

abstract class Animal {
    abstract void sound();  // Abstract method (no body)

    void eat() {            // Concrete method
        System.out.println("This animal eats food");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound();
        myDog.eat();
    }
}

Key Points:

  • An abstract class can have both abstract and non-abstract methods.
  • A class that extends an abstract class must implement all its abstract methods.
  • Abstract classes can have constructors and member variables.

3. Interfaces

  • An interface in Java is a reference type, similar to a class, that can contain only abstract methods (before Java 8) or default/static methods (from Java 8 onwards).
  • Interfaces are used to achieve full abstraction and are declared using the interface keyword.

Syntax:

interface Animal {
    void sound();  // Abstract method (implicitly public and abstract)
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound();
    }
}

Key Points:

  • Interfaces can only have method declarations (till Java 7) and fields (which are implicitly public, static, and final).
  • From Java 8, interfaces can have default methods with a body.
  • A class can implement multiple interfaces, thus supporting multiple inheritance.

Example with Default Method (Java 8 and above):

interface Animal {
    void sound();
    
    default void sleep() {
        System.out.println("This animal sleeps");
    }
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound();
        myDog.sleep();
    }
}

4. Differences Between Abstract Classes and Interfaces

Feature Abstract Class Interface
Keyword abstract interface
Inheritance Can extend one class (abstract or concrete) Can implement multiple interfaces
Methods Can have both abstract and concrete methods Only abstract methods (Java 7 and below)
Fields Can have instance variables Only static and final variables
Constructor Can have constructors Cannot have constructors
Usage For classes with shared code and abstract behavior For providing a contract for other classes

5. Advantages of Abstraction

  • Code Reusability: Promotes the reuse of existing code.
  • Flexibility: Enhances flexibility by allowing a single interface to be used with different types of objects.
  • Maintainability: Simplifies code maintenance by focusing on the essential details.

6. Disadvantages of Abstraction

  • Complexity: Can add complexity to the code.
  • Overhead: Abstract classes with abstract methods may introduce an overhead since subclasses must implement these methods.

7. Conclusion

Abstraction is a crucial concept in Java, promoting the separation of the interface from the implementation. It allows developers to focus on high-level design and functionality, improving code manageability and scalability.