nerdexam
Oracle

1Z0-808 · Question #91

Given: package pkg1; class Bb {} public class Ee { private Ee () {} } package pkg2; final class Ww; package pkg3; public abstract class Dd { void m() { } } And, 1. package pkg4; 2. import pkg1.; 3…

The correct answer is A. class Cc extends Bb {} D. class Cc extends Dd {}. D is correct because Dd is public abstract - it's designed to be subclassed, is accessible across packages, and has a package-private (but inherited) method m() that poses no barrier to compilation. B fails because Ww is declared final, which explicitly prohibits subclassing…

Working with Inheritance

Question

Given: package pkg1; class Bb {} public class Ee { private Ee () {} } package pkg2; final class Ww; package pkg3; public abstract class Dd { void m() { } } And,
  1. package pkg4;
  2. import pkg1.*;
  3. import pkg2.*;
  4. import pkg3.*;
  5. // insert a class definition here
Which two class definitions, when inserted independently at line 5, enable the code to compile?

Options

  • Aclass Cc extends Bb {}
  • Bclass Cc extends Ww {}
  • Cclass Cc extends Ee {}
  • Dclass Cc extends Dd {}

How the community answered

(22 responses)
  • A
    45% (10)
  • B
    14% (3)
  • C
    41% (9)

Explanation

D is correct because Dd is public abstract - it's designed to be subclassed, is accessible across packages, and has a package-private (but inherited) method m() that poses no barrier to compilation.

B fails because Ww is declared final, which explicitly prohibits subclassing - the compiler rejects any extends of a final class outright.

C fails because Ee's only constructor is private. Every subclass constructor must call super() implicitly or explicitly, but private Ee() is inaccessible outside Ee itself - making extension impossible.

Note on A: This is where the answer key appears to be in error. Bb has default (package-private) access in pkg1. From pkg4, import pkg1.* only imports public types, so Bb is not accessible - class Cc extends Bb {} should fail with a compilation error. If the question intended public class Bb {}, then A would compile. Treat this as a likely typo in the question source.

Memory tip: Think "FPC blocks extension" - Final classes can't be extended, Private constructors block subclassing, and Class visibility (package-private) blocks cross-package access. Only public non-final classes with accessible constructors are safe to extend from another package.

Topics

#Access Modifiers#Class Inheritance#Final Classes#Abstract Classes

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice