Abstraction is one of the four main principles of Object-Oriented Programming (OOP). It means hiding implementation details and showing only the essential features of an object.
Abstraction allows users to focus on what an object does instead of how it performs its work.
For example, when you use an ATM, you select an operation such as withdrawing money. You do not need to understand every internal banking process.
Java supports abstraction mainly through:
An abstract class is declared using the
abstract keyword. It can contain
abstract methods, concrete methods, fields,
and constructors.
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal is sleeping");
}
}
Here, Animal is an abstract class.
The sound() method has no body,
while sleep() has a body.
new Animal().
An abstract method is declared without a method body. A concrete subclass must implement inherited abstract methods unless that subclass is also abstract.
abstract class Shape {
abstract void draw();
}
The draw() method has no implementation
in the Shape class. Subclasses can provide their
own implementations.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.sound();
}
}
Dog barks
The Dog class provides the implementation of
the abstract sound() method.
abstract class Vehicle {
abstract void start();
abstract void stop();
void fuel() {
System.out.println("Vehicle needs energy");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts");
}
@Override
void stop() {
System.out.println("Car stops");
}
}
class Main {
public static void main(String[] args) {
Car c1 = new Car();
c1.start();
c1.fuel();
c1.stop();
}
}
Car starts
Vehicle needs energy
Car stops
The abstract class defines required operations and also provides a common concrete method.
Although an abstract class cannot be instantiated, its reference can refer to an object of a concrete subclass.
abstract class Animal {
abstract void sound();
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
class Main {
public static void main(String[] args) {
Animal a1 = new Cat();
a1.sound();
}
}
Cat meows
The reference type is Animal, but the actual object is Cat. Java calls the overridden method implemented by Cat.
An interface defines a contract that implementing
classes agree to follow. It is declared using
the interface keyword.
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.sound();
}
}
Dog barks
The Dog class implements the Animal interface
and provides a public implementation of
sound().
A Java class can implement more than one interface. This allows a class to follow multiple contracts.
interface Printable {
void print();
}
interface Scannable {
void scan();
}
class Machine implements Printable, Scannable {
@Override
public void print() {
System.out.println("Printing document");
}
@Override
public void scan() {
System.out.println("Scanning document");
}
}
class Main {
public static void main(String[] args) {
Machine m1 = new Machine();
m1.print();
m1.scan();
}
}
Printing document
Scanning document
The Machine class provides implementations for both interfaces.
| Abstract Class | Interface |
|---|---|
Declared using abstract class. |
Declared using interface. |
| Can have constructors. | Cannot have constructors. |
| Can have instance fields and concrete methods. | Fields are implicitly public, static, and final. |
| A class can extend only one class. | A class can implement multiple interfaces. |
| Useful for sharing state and common implementation. | Useful for defining a contract across different classes. |
abstract class Payment {
abstract void pay(double amount);
void showMessage() {
System.out.println("Processing payment");
}
}
class UpiPayment extends Payment {
@Override
void pay(double amount) {
System.out.println("Paid using UPI: " + amount);
}
}
class CardPayment extends Payment {
@Override
void pay(double amount) {
System.out.println("Paid using card: " + amount);
}
}
class Main {
public static void main(String[] args) {
Payment p1 = new UpiPayment();
Payment p2 = new CardPayment();
p1.showMessage();
p1.pay(500);
p2.showMessage();
p2.pay(1000);
}
}
Processing payment
Paid using UPI: 500.0
Processing payment
Paid using card: 1000.0
The Payment class defines the common structure, while each subclass provides its own payment behavior.
An abstract class can have a constructor. The constructor runs when a concrete subclass object is created.
abstract class Person {
String name;
Person(String name) {
this.name = name;
}
abstract void display();
}
class Student extends Person {
Student(String name) {
super(name);
}
@Override
void display() {
System.out.println("Student name: " + name);
}
}
class Main {
public static void main(String[] args) {
Student s1 = new Student("Rahul");
s1.display();
}
}
Student name: Rahul
The Student constructor calls the Person constructor
using super(name), initializing the
inherited name field.
1. Which keyword is used to declare an abstract class?
2. Which keyword is used when a class implements an interface?
3. Can an abstract class have a constructor?