Advanced⏱️ 11 min📘 Topic 13 of 22

🪞 Abstraction in Java — Abstract Classes vs Interfaces

Master abstraction in Java — abstract classes vs interfaces, default and static methods, functional interfaces, and when to use which. Updated for modern Java with examples.

Abstraction means exposing what something does while hiding how. Java offers two tools: abstract classes and interfaces.

🧱 Abstract classes

A class marked abstract can't be instantiated and may contain abstract methods (no body) plus concrete methods and state.

abstract class Shape {
  abstract double area();          // must be implemented
  void describe() { System.out.println("area=" + area()); }
}

🔌 Interfaces

An interface is a pure contract. A class implements it. Since Java 8, interfaces can have default and static methods with bodies:

interface Drawable {
  void draw();                       // abstract
  default void clear() { System.out.println("clearing"); } // default
  static Drawable empty() { return () -> {}; }            // static
}

⚖️ Abstract class vs interface

Abstract classInterface
State (fields)YesConstants only
ConstructorsYesNo
Multiple inheritanceNo (one superclass)Yes (implement many)
Use whenShared base + stateCapability/contract

💡 Rule of thumb

Use an interface to define a capability multiple unrelated classes can have (Comparable, Runnable). Use an abstract class when subtypes share state and a partial implementation. A single-method interface is a functional interface — the basis of lambdas, covered later.

💻 Code Examples

Abstract class with template method

abstract class Payment {
  abstract void charge(double amt);
  void process(double amt) {
    System.out.println("validating...");
    charge(amt);
  }
}
class Card extends Payment {
  void charge(double amt){ System.out.println("charged $" + amt); }
}
new Card().process(50);
Output:
validating...
charged $50.0

Implementing multiple interfaces

interface Flyer { void fly(); }
interface Swimmer { void swim(); }
class Duck implements Flyer, Swimmer {
  public void fly()  { System.out.println("flap"); }
  public void swim() { System.out.println("paddle"); }
}
Duck d = new Duck(); d.fly(); d.swim();
Output:
flap
paddle

Default method

interface Greeter {
  String name();
  default void greet() { System.out.println("Hi " + name()); }
}
Greeter g = () -> "Sam";
g.greet();
Output:
Hi Sam

⚠️ Common Mistakes

  • Trying to instantiate an abstract class — `new Shape()` is a compile error.
  • Forgetting interface methods are public — implementing methods must be public.
  • Adding state to an interface — only public static final constants are allowed.
  • Reaching for an abstract class when a simple interface (capability) would be cleaner.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What is the difference between an abstract class and an interface?Beginner
An abstract class can have state (fields), constructors, and concrete methods; a class extends only one. An interface is a contract — a class can implement many; since Java 8 it can have default/static methods but no instance state.
Q2.When would you use an abstract class over an interface?Intermediate
Use an abstract class when subtypes share common state and partial implementation (template method pattern). Use an interface to declare a capability that unrelated classes can provide, or when you need multiple inheritance of type.
Q3.What are default methods and why were they added?Intermediate
default methods (Java 8) let interfaces provide a method body. They were added so new methods could be added to existing interfaces (like Collection.stream()) without breaking all existing implementers.
Q4.Can a class implement two interfaces with the same default method?Advanced
Yes, but it must override the method to resolve the conflict, optionally calling InterfaceName.super.method() to pick one. Otherwise it's a compile error (the diamond problem for defaults).
Q5.What is a functional interface?Intermediate
An interface with exactly one abstract method (e.g., Runnable, Comparator, Function). It can be implemented with a lambda expression. Often annotated @FunctionalInterface to enforce the single-method rule.

🧠 Quick Summary

  • Abstraction exposes what, hides how.
  • Abstract class: state + constructors + partial implementation, single inheritance.
  • Interface: contract, multiple implementation, default/static methods (Java 8+).
  • Interface for capabilities; abstract class for shared base + state.
  • A single-abstract-method interface is a functional interface (lambda-ready).