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
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; }
}
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();
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
| Point | Abstract class | Interface |
|---|---|---|
| Keyword to use | extends | implements |
| How many | Only one | Many at once |
| Fields | Yes | Constants only |
| Normal methods | Yes | Mostly names (contract) |
| Best for | Shared code + is-a | A shared capability |
Common mistakes
- Instantiating an abstract class. You cannot use
newon it — extend it first. - Forgetting to implement all methods. A class must define every interface method (or be abstract).
- Mixing up extends and implements.
extendsfor classes,implementsfor 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?
What is an abstract class?
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?
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.