All topics
library
beginner

Abstraction in Java

Use abstract classes and interfaces to define contracts without exposing implementation.

Abstraction means showing what something does and hiding how it does it.

A TV remote hides the circuitry. You press volume up without knowing whether it uses infrared or Bluetooth.

Key Concepts

1
When you use a List, you know you can add items and fetch them by position. You do not know or care whether it stores them in an array or a chain of nodes. That is abstraction, and the benefit is that the hidden part can change without breaking your code.
List
2
Java gives you two tools: interfaces and abstract classes.An interface is a list of methods a class promises to provide. A class can implement many of them.
3
An abstract class is a partly-built class. It can hold fields and a constructor, and it can provide some methods while leaving others for subclasses. A class can extend only one.
4
Which to use. Ask what the relationship is.
5
If the types share data and a real "is a kind of" relationship — Manager is an Employee, both have a name and a salary — an abstract class fits, because it can hold those shared fields.
ManagerEmployee
6
If the types share only an ability, and are otherwise unrelated — anything can be Comparable, anything can be Runnable — an interface fits, and a class can pick up several.
ComparableRunnable
7
What changed in Java 8. Interfaces can now contain working methods, not just signatures:
8
    interface Vehicle {
        int wheels();                                    // must implement
        default String describe() {                      // provided
            return "vehicle with " + wheels() + " wheels";
        }
    }
9
This was added so that Collection could gain stream() without breaking every class in the world that implemented it.
Collectionstream()
10
So "interfaces cannot have any implementation" is no longer true. What interfaces still cannot have is instance fields — they hold no per-object data. That is now the real line between the two.
11
When two interfaces provide the same default method, the class must choose:
12
    class C implements A, B {
        public String hi() { return A.super.hi(); }   // required
    }
13
Adding an interface is not automatically abstraction. An interface with exactly one implementation, created because a guideline said so, hides nothing and doubles the files you read. The question to ask is whether a second implementation is plausible, or whether this is a boundary you want to keep stable — a database, a payment provider, a clock.
14
What the interviewer is probing.1. "Abstraction or encapsulation?" Probing: whether you can separate two words used interchangeably. Stalls: "They both hide things." Moves up: abstraction hides complexity behind a simpler view; encapsulation protects an object's internal state from outside interference.
15
2. "Interface or abstract class?" Probing: judgement. Stalls: "Interfaces are more flexible." Moves up: shared fields and a genuine is-a favour the abstract class; an ability shared by unrelated types favours the interface.
16
3. "Can an interface have a constructor?" Probing: the state boundary. Stalls: "No." Moves up: no, because there is no per-object data to set up — the same reason it has no instance fields.
17
4. "Two interfaces define the same default method." Probing: the conflict. Stalls: "It will not compile." Moves up: correct, and the class resolves it with A.super.method().
A.super.method()
18
5. "When would you not add an interface?" Probing: whether you apply the idea or the ritual. Stalls: "Always add one." Moves up: one implementation and no boundary to protect means indirection with no benefit.
19
  Interface vs abstract class, Java 8+
20
                            interface          abstract class
  ------------------------------------------------------------------
  Instance fields           no (constants only) yes
  Constructor               no                  yes
  Method bodies             default & static    yes
                            (and private,
                            Java 9+)
  How many per class        many                one
  Access modifiers          public (private
                            for helpers)        any
  Purpose                   capability /        shared state and
                            contract            behaviour
21
  Choosing
    "X can do Y"  and X types are unrelated     -> interface
    "X is a kind of Y" with shared fields       -> abstract class
    stable boundary you want to swap (DB, clock,
    payment provider)                           -> interface
    one implementation, no boundary             -> neither; use the
                                                   class
22
  The diamond, resolved explicitly
23
    interface A { default String hi() { return "A"; } }
    interface B { default String hi() { return "B"; } }
24
    class C implements A, B {
        public String hi() { return A.super.hi(); }   // required
    }