Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 6 OF 6 · 2 MIN READ

static, final, Abstract Classes and Interfaces

Keywords like static and final, and the choice between abstract classes and interfaces, are classic Java interview questions. Modern interfaces with default, static and private methods have changed the answer.

What you will learn

  • Explain static fields, methods, blocks and nested classes
  • Explain final for variables, methods and classes
  • Compare abstract classes and interfaces in modern Java
  • Know access modifiers

Key concepts

static

Belongs to the class, not to an object: one copy shared by all instances. Static methods cannot use this. Static blocks run once when the class loads.

final

A final variable is assigned once (the reference, not the object: a final List can still be modified). A final method cannot be overridden. A final class cannot be extended (String).

Abstract class

Can have state (fields), constructors and both abstract and concrete methods. A class extends only one.

Interface

A contract. Can have abstract, default, static and private methods and constants, but no instance state. A class implements many. Sealed interfaces (Java 17) can restrict which classes implement them.

Step by step

Access modifiers

modifier     class  package  subclass  world
public         ✓       ✓        ✓        ✓
protected      ✓       ✓        ✓        ✗
(default)      ✓       ✓        ✗        ✗
private        ✓       ✗        ✗        ✗

Examples

Interface with default and static methods

public interface Discount {
    BigDecimal apply(BigDecimal price);

    default Discount andThen(Discount next) {
        return p -> next.apply(apply(p));
    }

    static Discount percent(int pct) {
        return p -> p.multiply(BigDecimal.valueOf(100 - pct)).divide(BigDecimal.valueOf(100));
    }
}

Discount festive = Discount.percent(10).andThen(Discount.percent(5));

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Show that a final List can still be modified and make it truly unmodifiable.
  • Explain the order in which static blocks, instance blocks and constructors run.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Abstract class or interface?

Use an interface to define a capability that unrelated classes can share (Comparable, PaymentGateway). Use an abstract class when subclasses share state and code and are truly the same kind of thing (a template method with fields).

Why were default methods added?

To evolve interfaces without breaking every implementation. Java 8 needed to add stream() and forEach() to Collection; default methods let existing classes inherit them.

Summary

static belongs to the class, final prevents change, abstract classes share state and code, interfaces define contracts with default, static and private methods, and access modifiers control visibility.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus