ProgrammingModerateProduct-company placement · Java
A base type must hold common fields, have a constructor that validates them, and share some method bodies, while subclasses form a strict is-a hierarchy. Which Java construct fits best?
- A. An interface with default methods
- B. A final class
- C. An enum
- D. An abstract class
Correct answer: D. An abstract class
Interfaces cannot have instance fields or constructors; default methods can share behaviour but not state. An abstract class provides state, constructors and partial implementation while forbidding direct instantiation.
More OOP with C++ and Java questions
- java.io.BufferedReader wraps another Reader, is itself a Reader, and adds buffering on top of it; such wrappers can be stacked at run…
- Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any…
- Square extends Rectangle and overrides setWidth and setHeight so that both always change together. Client code written for Rectangle does…
- A Department has Professors who continue to exist if the department is closed, while a House is made of Rooms that cannot exist without…
- What is printed? java // static methods of the main class: static void p(Object o) { System.out.print("O"); } static void p(String s) {…
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2)); for (Map.Entry<String, Integer> e : m.entrySet())…