nerdexam
Oracle

1Z0-803 · Question #108

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. i

The correct answer is A. class Cc extends Bb { } D. class Cc extends Dd { }. This question tests the understanding of Java class modifiers, constructor visibility, and inheritance rules when classes are defined across packages.

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

(32 responses)
  • A
    69% (22)
  • B
    22% (7)
  • C
    9% (3)

Why each option

This question tests the understanding of Java class modifiers, constructor visibility, and inheritance rules when classes are defined across packages.

Aclass Cc extends Bb { }Correct

Class `Bb` is a non-final, non-abstract class with a default constructor, making it accessible and extendable by class `Cc` through package import.

Bclass Cc extends Ww { }

Class `Ww` is declared with the 'final' modifier, which explicitly prevents any other class from extending it.

Cclass Cc extends Ee { }

Class `Ee` has a private constructor, preventing any external class, including a subclass, from implicitly or explicitly invoking a superclass constructor during inheritance.

Dclass Cc extends Dd { }Correct

Class `Dd` is an abstract class, explicitly designed for extension, and since it's public and imported, `Cc` can extend it without issue.

Concept tested: Java class inheritance rules and modifiers

Source: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Topics

#inheritance#access modifiers#packages#constructors

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice