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:
- Abstract Classes
- Interfaces
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 theAnimal
class is abstract, so it doesn’t have an implementation. - The
Dog
class extendsAnimal
and provides a concrete implementation of thesound()
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
orstatic
). - A class can implement multiple interfaces, allowing multiple inheritance of behavior.
- Interfaces are used to define a contract that implementing classes must adhere to.
- All methods in an interface are implicitly abstract (unless defined as
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 abstractsound()
method and a defaultsleep()
method. - The
Dog
class implements theAnimal
interface and provides an implementation for thesound()
method. - The
sleep()
method is inherited from theAnimal
interface and does not need to be overridden unless required.
Key Differences Between Abstract Class and Interface:
Feature Abstract Class Interface Purpose Used to represent a class that shares common behavior but should not be instantiated directly. Used to represent a contract that classes can implement. Methods Can have both abstract (without implementation) and concrete (with implementation) methods. Can only have abstract methods (unless using Java 8’s default
methods orstatic
methods).Constructors Can have constructors. Cannot have constructors. Multiple Inheritance A class can inherit from only one abstract class. A class can implement multiple interfaces. Access Modifiers Can have any access modifier (private, protected, public). All methods in an interface are implicitly public
.Fields Can have instance variables (fields). Can only have constants (implicitly public static final
).Keyword Used Use the abstract
keyword.Use the interface
keyword.Default Methods Cannot 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.