Java Class Design and OOP Flashcards
7 cards from real OCP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Java Class Design and OOP flashcards as text
Which of the following correctly defines an enum with a constructor in Java?
Answer: enum Color { RED(1), GREEN(2), BLUE(3); private int val; Color(int val){ this.val=val; } }
Enum constructors are implicitly private and are called with arguments per constant; fields hold per-constant data.
What is the result when an abstract class implements an interface but does not implement all of its methods?
Answer: The unimplemented methods become abstract in the abstract class
An abstract class is not required to implement all interface methods; unimplemented methods remain abstract and must be implemented by the first concrete subclass.
Which of the following is an example of the 'is-a' relationship best implemented with inheritance?
Answer: Dog is-a Animal
Inheritance models 'is-a' relationships; a Dog IS an Animal, making inheritance the appropriate design.
When using `@Override`, what happens if the method signature does not match any method in the superclass or interface?
Answer: A compilation error occurs
`@Override` causes a compile-time error if the annotated method does not actually override a superclass or interface method.
What is the output of this code? ```java interface Flyable { default String move() { return "fly"; } } interface Swimmable { default String move() { return "swim"; } } class Duck implements Flyable, Swimmable { public String move() { return Flyable.super.move(); } } System.out.println(new Duck().move()); ```
Answer: fly
Duck resolves the ambiguity by explicitly calling `Flyable.super.move()`, which returns "fly".
Which statement about sealed classes (Java 17+) is correct?
Answer: Sealed classes restrict which classes can extend them using the permits clause
A sealed class uses the `permits` clause to explicitly list which classes are allowed to extend it.
What does it mean for a class to be 'immutable' in Java?
Answer: Its state cannot be changed after construction
An immutable class's state cannot be modified after its object is created — typically achieved with final fields and no setters.