Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 21

Abstraction and Interface in Java

In this lesson: Achieve abstraction in Java using abstract classes and interfaces — differences, rules and examples. In Hindi & English.

What is abstraction

Abstraction means hiding the details and showing only what matters. A car's pedal is a simple interface — you press it without knowing the engine's inner workings. In Java, abstraction is achieved with abstract classes and interfaces, which describe what to do, leaving each class to decide how.

💡 Abstract class = partial blueprint with some code. Interface = a pure contract of what a class must do.

Abstract class

abstract class Shape {
    abstract double area();          // no body — must be filled
    void describe() {                // normal method with code
        System.out.println("Area: " + area());
    }
}
class Circle extends Shape {
    double r = 5;
    double area() { return 3.14 * r * r; }
}
Area: 78.5

An abstract class cannot be created directly. It can mix abstract methods (no body) with normal ones. A child extends it and must complete the abstract methods.

Interface

interface Animal {
    void sound();        // method names only (a contract)
    void eat();
}

An interface is a pure contract — a list of methods a class promises to provide, with no code inside (traditionally). It says what must exist, not how it works.

Implementing an interface

class Dog implements Animal {
    public void sound() { System.out.println("Woof"); }
    public void eat()   { System.out.println("Dog eating"); }
}
// use:
Animal a = new Dog();
a.sound();
Woof

A class uses implements to fulfil an interface, and must define every method the interface lists. This guarantees any Animal has a sound() and eat().

Many interfaces

interface Swimmer { void swim(); }
interface Walker  { void walk(); }

class Duck implements Swimmer, Walker {
    public void swim() { System.out.println("swimming"); }
    public void walk() { System.out.println("walking"); }
}

A class can implement several interfaces at once — something not possible with classes. This is how Java combines capabilities without multiple class inheritance.

Abstract class vs interface

PointAbstract classInterface
Keyword to useextendsimplements
How manyOnly oneMany at once
FieldsYesConstants only
Normal methodsYesMostly names (contract)
Best forShared code + is-aA shared capability

Common mistakes

  • Instantiating an abstract class. You cannot use new on it — extend it first.
  • Forgetting to implement all methods. A class must define every interface method (or be abstract).
  • Mixing up extends and implements. extends for classes, implements for interfaces.
  • Trying to extend two classes. Use multiple interfaces instead.

Summary

  • Abstraction hides details and shows only the essentials.
  • An abstract class mixes normal and abstract methods; extend it with extends.
  • An interface is a contract of methods; fulfil it with implements.
  • A class can implement many interfaces but extend only one class.
  • Use abstract classes for shared code, interfaces for shared capabilities.

Next, revise inheritance, or control access with access modifiers.

Frequently Asked Questions

What is abstraction in Java?
Abstraction means hiding the internal details and showing only the essential features. In Java you achieve it with abstract classes and interfaces, which say what a class must do without saying how, letting each class fill in the details.
What is an abstract class?
An abstract class is a class marked abstract that cannot be instantiated on its own. It can have both normal methods (with code) and abstract methods (just a name, no body) that child classes must complete.
What is the difference between an abstract class and an interface?
An abstract class can have fields and normal methods and is extended with extends (one only). An interface is mostly method names, implemented with implements, and a class can implement many interfaces.
What is the difference between extends and implements?
extends is used to inherit from a class (including an abstract class), and you can extend only one. implements is used to fulfil an interface, and a class can implement several interfaces.
When should I use an interface vs an abstract class?
Use an abstract class when classes share common code and a clear "is-a" relationship. Use an interface to define a capability that unrelated classes can share, or when you need a class to have several such capabilities.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicEncapsulation Your progress: 21 / 61 (34%) · Lesson 22 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free