Java — Which two are legal declarations for non-nested classes and interfaces?

Java Programming Declarations and Access Control Difficulty: Medium
Choose an option
  • A
    final abstract class Test {}
  • B
    public static interface Test {}
  • C
    final public class Test {}
  • D
    protected abstract class Test {}
  • E
    protected interface Test {}

Answer

Correct Answer: final public class Test {} and abstract public class Test {}

Explanation

Introduction / Context:This evaluates legal top-level class and interface declarations.

Concept / Approach:

  • A top-level class can be public or default only (not protected/private).
  • It cannot be both final and abstract at the same time (contradiction).
  • Interfaces cannot be static at the top level.
  • abstract public class Test {} is legal.
  • final public class Test {} is legal.

Final Answer:final public class Test {} and abstract public class Test {}

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion