Abstraction in Java is a key concept in object-oriented programming (OOP) that focuses on hiding the implementation details and showing only the essential features of an object. It allows you to work with objects at a higher level, focusing on what an object does rather than how it does it. Java provides abstraction primarily through abstract classes and interfaces.
An abstract method in Java is a method that does not have a body; instead, it’s a method declaration that must be implemented by any subclass that extends the abstract class. In other words, an abstract method represents behavior that the subclass is expected to define. Abstract methods are declared using the abstract
keyword, and they exist only in abstract classes or interfaces.
An abstract method has the following syntax:
abstract class ClassName {
// Abstract method
public abstract void methodName();
}
Here:
methodName()
is declared as abstract
and has no body.ClassName
must also be declared as abstract
, because it contains an abstract method.// Abstract class with an abstract method
abstract class Animal {
// Abstract method (does not have a body)
public abstract void sound();
// Regular method with a body
public void breathe() {
System.out.println("Breathing...");
}
}
// Subclass provides the implementation for the abstract method
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
@Override
public void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound(); // Output: Bark
dog.breathe(); // Output: Breathing...
Animal cat = new Cat();
cat.sound(); // Output: Meow
cat.breathe(); // Output: Breathing...
}
}
Animal
:Animal
is declared as abstract
because it contains an abstract method sound()
.breathe()
is a concrete method, so it can be inherited and used by subclasses as is.Dog
and Cat
:Dog
and Cat
classes extend the Animal
abstract class and provide concrete implementations for the sound()
method.sound()
method, as it is abstract in the parent class.dog
and cat
objects are instances of Dog
and Cat
, but they are referenced by the Animal
type. This allows us to call sound()
polymorphically, with each subclass providing its own behavior for the method.Animal
class above, the breathe()
method is shared by all animals, while each subclass is responsible for implementing its specific behavior through the sound()
method.Both abstract classes and interfaces are tools for achieving abstraction in Java, but there are key differences:
public
, static
, and final
(i.e., constants).Animal animal = new Animal();
would result in a compilation error. You must create an instance of a subclass that provides concrete implementations of all abstract methods.Dog
did not implement the sound()
method, it would need to be declared abstract.protected
or public
. They cannot be private
because they are meant to be overridden by subclasses, and private methods are not accessible to subclasses.static
cannot also be marked as abstract
because static methods belong to the class itself and are not meant to be overridden by subclasses.An abstract class can have both abstract methods (without implementation) and concrete methods (with implementation). This allows you to define some default behavior in the abstract class while leaving certain behaviors to be defined by subclasses.
abstract class Vehicle {
// Abstract method
public abstract void start();
// Concrete method
public void stop() {
System.out.println("Vehicle stopped");
}
}
class Car extends Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
car.start(); // Output: Car started
car.stop(); // Output: Vehicle stopped
}
}
In this example:
Vehicle
class defines an abstract method start()
and a concrete method stop()
.Car
subclass provides the implementation for the abstract method start()
, while it inherits the stop()
method from Vehicle
.public
or protected
because they are meant to be overridden by subclasses.Abstraction through abstract methods in Java allows for the separation of method definitions from their implementations. This promotes flexibility and reusability by defining a common interface for subclasses to implement while allowing each subclass to provide its specific implementation.