Classes, records, sealed types and enums: Java 25 practice questions

Classes, records, sealed types and enums is 16% of the free Java 25 mock (8 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.

J.II · Java 25 practice question

Class A declares a package-access instance method mA. Subclass C is declared in a different package and defines an instance method mC with the same signature. Does mC override mA?

Choose one answer
Check answer and explanation

Correct answer: No, because a package-access method can only be overridden by a subclass in the same package.

Explanation

The method mC does not override mA because overriding a package-access method requires mA to be declared in the same package as C.

Common trap

Package-access methods are invisible outside their package, so subclasses in other packages declare an unrelated method rather than overriding. Widening mC to public does not create an override relationship when mA is package-access in another package.

Source: JLS SE 25 §8.4.8.1. Overriding (by Instance Methods)
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

Class A declares a private instance method mA, and subclass C in the same package declares an instance method mC with the exact same signature. Does mC override mA?

Choose one answer
Check answer and explanation

Correct answer: No, because an overridden method in A must be public, protected, or have package access in the same package.

Explanation

The method mC does not override mA because overriding requires mA to be public, protected, or declared with package access in the same package as C. A private method is never overridden.

Common trap

Being in the same package enables overriding package-access methods, not private methods. Not inheriting mA and having a subsignature are necessary conditions, but the accessibility requirement on mA must also be satisfied.

Source: JLS SE 25 §8.4.8.1. Overriding (by Instance Methods)
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

A developer compares a record instance against an instance of a different record type that happens to have the exact same component names and values. What does the implicitly declared equals(Object) method return?

record Size(int w, int h) {}
record Dim(int w, int h) {}
void main() {
    IO.println(new Size(10, 20).equals(new Dim(10, 20)));
}
Choose one answer
Check answer and explanation

Correct answer: false

Explanation

The program prints false because the record's equals(Object) method requires the argument to be an instance of the same record class R; otherwise it returns false.

Common trap

Records use nominal typing rather than structural typing, so matching component names and values across different record classes do not make them equal. The equals(Object) method checks the argument's type safely and returns false rather than throwing a ClassCastException.

Source: JLS SE 25 §8.10.3. Record Members
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

A developer creates an interface that extends two superinterfaces, each providing a default method with the same signature, without declaring that method in the subinterface. How does the Java 25 compiler handle this?

Choose one answer
Check answer and explanation

Correct answer: It reports a compile-time error because a default method is override-equivalent with another inherited method.

Explanation

The compiler reports a compile-time error whenever an interface inherits a default method that is override-equivalent with another inherited method, whether that other method is default or abstract.

Common trap

The order of superinterfaces in an extends clause never breaks ties between conflicting default methods in Java. Conflict detection happens on the subinterface declaration itself, not later when a class implements it.

Source: JLS SE 25 §9.4.1.3. Inheriting Methods with Override-Equivalent Signatures
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

A developer declares an interface method without private, default, or static, and writes an empty block body for it. What happens when compiling this code?

interface Greeter {
    void greet() {}
}
void main() {
    IO.println("done");
}
Choose one answer
Check answer and explanation

Correct answer: Compilation fails because the method is implicitly abstract and its body must be a semicolon, not a block.

Explanation

Compilation fails because an interface method lacking private, default, or static is implicitly abstract, and an abstract method's body must be represented by a semicolon rather than a block.

Common trap

Java never infers the default modifier from the presence of a method body; default must be written explicitly. An empty block is still a method body and is illegal on an implicitly abstract interface method.

Source: JLS SE 25 §9.4. Method Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

A developer defines an interface that extends two superinterfaces declaring override-equivalent methods, where one is a default method and the other is abstract. What happens when this Java 25 source file is compiled?

interface A {
    default String label() { return "A"; }
}
interface B {
    String label();
}
interface C extends A, B {}
void main() {
    IO.println("ok");
}
Choose one answer
Check answer and explanation

Correct answer: Compilation fails because the subinterface inherits a default method whose signature is override-equivalent with another inherited method.

Explanation

Compilation fails because an interface cannot inherit a default method that is override-equivalent with another inherited method, even when the second method is abstract. The subinterface must explicitly declare its own overriding method to resolve the conflict.

Common trap

Developers often assume a default implementation automatically fills in an inherited abstract method, but Java requires an explicit override in the subinterface. Treating the conflict as implicitly abstract is a common misconception; the compiler rejects the declaration instead.

Source: JLS SE 25 §9.4.1.3. Inheriting Methods with Override-Equivalent Signatures
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

For an instance method mC declared in or inherited by class C to override an instance method mA declared in class A, which structural condition must hold?

Choose one answer
Check answer and explanation

Correct answer: C must be a subclass of A, C must not inherit mA, and mC's signature must be a subsignature of mA's signature.

Explanation

Overriding requires that C is a subclass of A, C does not inherit mA, and the signature of mC is a subsignature of the signature of mA as a member of the supertype of C that names A.

Common trap

C does not have to be a direct subclass of A, and C specifically must not inherit mA for mC to override it. mC can be inherited by C rather than declared directly, and mA does not need to be abstract.

Source: JLS SE 25 §8.4.8.1. Overriding (by Instance Methods)
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

Interface I has two direct superinterfaces J and J'. Method m is a member of J, and method m' is a member of J' such that m' overrides m. Does interface I inherit m from J?

Choose one answer
Check answer and explanation

Correct answer: No, because another direct superinterface J' has a method m' that overrides m.

Explanation

Interface I does not inherit m from J because there exists a method m' in another direct superinterface J' that overrides the declaration of m.

Common trap

Overriding between sibling direct superinterfaces filters out the overridden method m so I only inherits the more specific m'. Because m is not inherited when m' overrides it, no conflict error occurs from inheriting both.

Source: JLS SE 25 §9.4.1. Inheritance and Overriding
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

Which modifier combination is forbidden on an interface method declaration in Java 25?

Choose one answer
Check answer and explanation

Correct answer: Declaring an interface method as synchronized or final.

Explanation

An interface method may not be declared with the modifiers final, synchronized, or native, so declaring an interface method as synchronized or final is forbidden.

Common trap

Private helper methods with block bodies have been permitted in interfaces since Java 9. Static methods with block bodies have been permitted in interfaces since Java 8.

Source: JLS SE 25 §9.4. Method Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

In a normal class declaration, a developer writes a constructor with no access modifier at all. What accessibility does that constructor have?

Choose one answer
Check answer and explanation

Correct answer: Package access, making it accessible within the same package.

Explanation

In a normal class declaration, a constructor declared with no access modifiers has package access.

Common trap

Only a compiler-generated default constructor mirrors the class's public modifier; an explicitly written constructor with no modifier gets package access. Omitting an access modifier in Java never grants protected access to subclasses in other packages.

Source: JLS SE 25 §8.8.3. Constructor Modifiers
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

Suppose inner class S is a member of class C due to inheritance, and C invokes S's superclass constructor. Can this itself (the zeroth lexically enclosing instance) be used as the immediately enclosing instance with respect to S?

Choose one answer
Check answer and explanation

Correct answer: No, the zeroth lexically enclosing instance of this is never used as the immediately enclosing instance with respect to S.

Explanation

Even when S is a member of C through inheritance, the zeroth lexically enclosing instance of this (this itself) is never used as the immediately enclosing instance of the new object with respect to S.

Common trap

Inherited membership makes it tempting to treat this as the enclosing instance, but the JLS explicitly requires the n'th lexically enclosing instance with n at least 1. Whether C is static or not does not allow this itself to serve as the immediately enclosing instance.

Source: JLS SE 25 §8.8.7.1. Constructor Invocations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.II · Java 25 practice question

A developer tries to restrict an interface method to subclasses by marking it protected. What is the result of compiling this Java 25 source file?

interface Service {
    protected void execute();
}
void main() {
    IO.println("ok");
}
Choose one answer
Check answer and explanation

Correct answer: Compilation fails because an interface method may not be declared with protected access.

Explanation

Compilation fails because the Java Language Specification forbids declaring an interface method with protected access.

Common trap

While classes allow protected methods for subclass visibility, interfaces only permit public or private methods. Adding default does not make protected legal on an interface method.

Source: JLS SE 25 §9.4. Method Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


Back to all topics