skip to content

Abstraction (Abstract Classes & Interfaces)

Java's two abstraction tools — abstract classes and interfaces — including default, static and private interface methods. Choosing between them is a standard design question at every level.

part ofJavaoverview, primer and where to startread it →
on this pageshow

explore

questions

25

How do you declare an abstract method, and what obligations does it place on subclasses?

level: juniorimportance: must knowfreq 70%

answer

  1. abstract + signature + semicolon, no body
  2. only in abstract class or interface
  3. first concrete subclass must override
  4. cannot be private, static, or final
  5. compiler enforces the contract

basics

~20 s

An abstract method is written with the abstract keyword and no body — just a signature ending in a semicolon. Any concrete (non-abstract) subclass must override it and supply a real body, or it won't compile.

solid answer

~40 s

You declare an abstract method by marking it abstract and giving it a signature with no body, terminated by a semicolon instead of braces: `abstract double area();`. It only legally appears inside an abstract class (or as the implicit form in an interface). The obligation it creates: the first concrete subclass in the chain must override it with a matching signature and provide a body. If a subclass leaves it unimplemented, that subclass must itself be declared abstract, pushing the obligation further down. Abstract methods cannot be private (subclasses couldn't see them to override), cannot be static (static methods aren't overridden), and cannot be final (final forbids overriding). The point is to define a contract — 'this operation exists and varies per subclass' — while deferring the implementation.

go deeper

for a junior

Writes the syntax correctly (abstract keyword, semicolon, no body) and knows a subclass must fill it in.

for a middle

Explains the deferral chain (abstract subclass may skip; first concrete one must override) and the private/static/final restrictions with reasons.

for a senior

Connects abstract methods to Template Method / skeletal implementations and overriding rules (covariant returns, exception narrowing).

for a principal

Reasons about contract design — minimizing the abstract surface, the fragility of base classes that call their own abstract hooks during construction, and migration to default methods/composition.

## Anatomy of an abstract method A normal Java method has a **signature** (modifiers, return type, name, parameter list) followed by a **body** in braces: ```java double area() { return 3.14; } ``` An **abstract method** keeps the signature but replaces the body with a single semicolon, and adds the `abstract` keyword: ```java abstract double area(); ``` This declares 'there exists an operation called `area` that takes no arguments and returns a double, but I am not saying how it works.' It is a *promise of behavior* with no implementation. ## Where it can appear An abstract method may only live inside an **abstract class** (a class marked `abstract`) or an **interface** (where method declarations are implicitly abstract unless marked `default`/`static`). Putting an abstract method in a normal class is a compile error, because the class would then be instantiable yet have a method with no behavior. ## The obligation on subclasses When class `B extends A` and `A` has an abstract method, Java tracks whether `B` provides an implementation: - If `B` **overrides** the abstract method with a concrete body (matching signature), `B` can be a normal, instantiable class. - If `B` does **not** implement it, then `B` still has an inherited abstract method, so **`B` must itself be declared `abstract`**. The obligation is inherited and passed down the chain until some subclass finally implements it. This is what makes abstract methods a *contract*: the compiler guarantees that you can never create an object that has an unimplemented abstract method. **Overriding** means supplying a subclass method with the same name and parameters; the abstract method is the 'slot' the override fills. ## Modifier restrictions (and why) Abstract methods cannot combine with certain modifiers because the combinations are contradictory: - **`private`** — private members aren't visible to subclasses, so they couldn't be overridden. Contradiction. - **`static`** — static methods belong to the class, not an instance, and are *hidden*, not overridden via dynamic dispatch. An abstract static method has no one to implement it polymorphically. Contradiction. - **`final`** — `final` means 'cannot be overridden,' the exact opposite of abstract's 'must be overridden.' Contradiction. All three produce compile errors. ## Why this matters Abstract methods are the mechanism behind the **Template Method** pattern and 'skeletal implementations': the base class writes the overall algorithm in a concrete method and calls abstract 'hook' methods that subclasses fill in. The base controls the *structure*; subclasses control the *steps*.

  • Why can't an abstract method be private?
    Private members aren't inherited or visible to subclasses, so no subclass could ever override and implement it. An abstract method that can never be implemented is meaningless, so the combination is a compile error.
  • If A is abstract with method m(), and B extends A without implementing m(), is B legal?
    Yes, as long as B is also declared abstract. B inherits the still-unimplemented m(), so it cannot be concrete. The implementation obligation moves down to the first concrete subclass of B.

saying these in an interview costs you the question

  • Writing an abstract method with empty braces {} — that's a concrete no-op method, not abstract.
  • Saying every subclass must implement it — only the first *concrete* subclass must; abstract subclasses may defer.
  • Thinking abstract methods can be private or static.
  • Forgetting that an abstract method ends with a semicolon, not braces.

context

open as a page

What is an abstract class in Java, and what can it contain that a regular interface (historically) could not?

level: juniorimportance: must knowfreq 78%

basics

~20 s

An abstract class is a class marked with the abstract keyword that you cannot create objects from directly. It can mix finished methods with unfinished (abstract) methods that subclasses must complete. It can also hold normal fields and constructors.

open as a page

How do you declare an interface in Java, and what can it contain?

level: juniorimportance: must knowfreq 75%

basics

~20 s

You write interface Name { ... }. An interface lists method signatures (no bodies) that implementing classes must provide, and it can hold constants. A class uses implements Name to promise it provides those methods.

open as a page

What are default methods in a Java interface, why were they added, and how do you declare one?

level: juniorimportance: must knowfreq 70%

basics

~20 s

A default method is an interface method with a body, marked with the default keyword. Classes that implement the interface inherit it for free, so you can add new methods to an interface without breaking existing implementers.

open as a page

What is a functional interface in Java, and what does the @FunctionalInterface annotation do?

level: juniorimportance: must knowfreq 78%

basics

~20 s

A functional interface is an interface with exactly one abstract method, so it can be implemented by a lambda. @FunctionalInterface is an optional annotation that makes the compiler check that the interface really has just one abstract method.

open as a page

What are the core differences between an interface and an abstract class in Java, and when would you reach for each?

level: juniorimportance: must knowfreq 88%

basics

~20 s

An interface describes what a class can do (a contract of methods); an abstract class is a partly-finished base class you extend. A class can implement many interfaces but extend only one class. Use an interface for a capability shared by unrelated types; use an abstract class when subclasses share state and code.

open as a page

What are the implicit (default) modifiers on interface methods and fields, and why does this matter?

level: middleimportance: must knowfreq 68%

basics

~10 s

In an interface, abstract methods are automatically public abstract, and fields are automatically public static final (constants). You don't have to write those modifiers; the compiler adds them.

open as a page

How does a class implement multiple interfaces, and how does this differ from extending classes?

level: middleimportance: must knowfreq 70%

basics

~10 s

List several interfaces separated by commas: class Car implements Drivable, Serviceable. The class must provide methods for all of them. A class can implement many interfaces but can extends only one class.

open as a page

Given a real modeling scenario, how do you decide between an interface, an abstract class, or both — what concrete signals push you each way?

level: middleimportance: must knowfreq 68%

basics

~20 s

Ask: is this a capability many unrelated types can have (use an interface), or a family of related types that share data and code (use an abstract class)? If types are unrelated or already extend something, choose an interface. If they're a real is-a family with shared fields, choose an abstract class — or expose an interface and back it with an abstract base.

open as a page

When subclasses need to share mutable state and initialization logic, why does an abstract class fit better than an interface?

level: middleimportance: must knowfreq 72%

basics

~20 s

An abstract class can have instance fields and a constructor, so it can store shared data per object and set it up when the object is created. An interface can't hold per-object data or have a constructor, so it can't manage shared mutable state.

open as a page

A class implements two interfaces that each provide a default method with the same signature. What happens, and how do you resolve it?

level: seniorimportance: must knowfreq 60%

basics

~10 s

If two interfaces give the same default method, the class won't compile until you resolve the clash. You override the method in the class, and you can pick one parent's version with InterfaceName.super.method().

open as a page

What is a marker interface in Java? Give examples and explain how it works.

level: juniorimportance: should knowfreq 62%

basics

~20 s

A marker interface is an empty interface with no methods. A class implements it just to 'tag' itself with a property, like being serializable. Code elsewhere checks 'instanceof' to see if the tag is present and behaves differently. Serializable and Cloneable are examples.

open as a page

Why can't you instantiate an abstract class, yet it can still declare constructors and even fields — what role do those play?

level: middleimportance: should knowfreq 58%

basics

~20 s

You can't use new on an abstract class because it may have unfinished (abstract) methods, so the object would be incomplete. Its constructor and fields still exist to set up the shared state that subclasses inherit; the subclass constructor calls super(...) to run them.

open as a page

What are private and private static methods in an interface, and what problem do they solve?

level: middleimportance: should knowfreq 40%

basics

~20 s

Since Java 9 an interface can have private methods (and private static methods). They are internal helpers that hold shared logic used by the interface's default or static methods, without exposing that logic to implementing classes.

open as a page

How do static methods in an interface differ from default methods, and how are they invoked?

level: middleimportance: should knowfreq 55%

basics

~10 s

A static interface method belongs to the interface itself, not to any instance. You call it as Interface.method(...). Unlike a default method, it is not inherited by implementing classes and cannot be overridden.

open as a page

How do abstract classes enable the Template Method pattern, and why is a concrete method calling an abstract one the core mechanism?

level: seniorimportance: should knowfreq 52%

basics

~20 s

The abstract class writes the overall algorithm once in a concrete method, but leaves the varying steps as abstract methods it calls. Subclasses fill in only those steps. The base controls the order and structure; subclasses control the details.

open as a page

What is a functional interface and what is a marker interface? How do they relate to interface declaration?

level: seniorimportance: should knowfreq 55%

basics

~20 s

A functional interface has exactly one abstract method, so it can be implemented with a lambda (e.g. Runnable). A marker interface has no methods at all; it just tags a class with a capability (e.g. Serializable).

open as a page

When would you choose an interface over an abstract class for abstraction in Java?

level: seniorimportance: should knowfreq 72%

basics

~20 s

Use an interface to define a capability that unrelated types can share, since a class can implement many interfaces. Use an abstract class when you need shared state, constructors, or partial implementation across closely related types.

open as a page

How does the compiler decide which functional interface a lambda or method reference implements, and why can the same lambda satisfy multiple functional interfaces?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A lambda has no type of its own. The compiler looks at the context where the lambda is used — the variable type, the method parameter, or the return type — to find the expected functional interface, then checks the lambda matches that interface's single method. The same lambda fits any interface whose method has a compatible shape.

open as a page

Marker interfaces vs annotations for tagging types: what are the trade-offs, and when would you choose each?

level: seniorimportance: should knowfreq 48%

basics

~20 s

A marker interface tags a type and gives you a real type the compiler can use (e.g. require it as a method parameter). An annotation can tag types, methods, or fields and can carry data, but doesn't create a type for compile-time checks. Use a marker for type-level guarantees; an annotation for richer metadata.

open as a page

How does Java resolve a conflict when a class inherits the same default method from two interfaces, and how does this compare to inheriting from a class?

level: seniorimportance: should knowfreq 55%

basics

~20 s

If two interfaces give the same default method, Java won't pick for you — the class must override that method, and inside it can call a chosen one with InterfaceName.super.method(). With classes there's only one superclass, so no such conflict arises.

open as a page

When evolving a published interface, what are the risks of adding a default method, and how do default/static/private methods change how you design interface APIs?

level: principalimportance: should knowfreq 30%

basics

~20 s

Adding a default method lets existing implementers keep compiling, but the default might not be correct for every implementation, and it can clash with methods those classes already have. Use default/static/private methods to evolve and organize an interface, but don't put real state or business logic in them.

open as a page

From an API-design and evolution standpoint, what are the trade-offs of exposing an interface versus an abstract class as your public type, and how do default methods change the calculus?

level: principalimportance: should knowfreq 42%

basics

~20 s

An interface is a flexible public contract: clients can implement it freely and you can add new methods later via default methods without breaking them. An abstract class lets you share code but locks implementers into your single-inheritance hierarchy. For public APIs, prefer an interface and optionally ship an abstract 'skeleton' class for reuse.

open as a page

When should you choose an abstract class over an interface or a concrete class, and what are the long-term costs of exposing an abstract class in a public API?

level: principalimportance: nice to knowfreq 40%

basics

~20 s

Use an abstract class when subclasses share real state and implementation under a single 'is-a' hierarchy. Prefer an interface when you only need a contract or multiple inheritance of type. Exposing an abstract class in a public API locks you into its structure, so favor composition where you can.

open as a page

As a marker interface, what hidden contract and design risks does implementing java.io.Serializable impose?

level: principalimportance: nice to knowfreq 33%

basics

~20 s

Serializable looks like a harmless empty tag, but implementing it secretly makes your object's private fields part of a public, long-lived format. That makes the class hard to change later, opens security holes when reading untrusted bytes, and weakens encapsulation. So you should add it deliberately, not by habit.

open as a page