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 keyword prevents a method from being overridden in a subclass?
Answer: final
The `final` keyword on a method prevents subclasses from overriding it.
What happens when a class is declared `abstract` and contains no abstract methods?
Answer: The class cannot be instantiated but can be subclassed
An abstract class with no abstract methods is valid — it simply cannot be instantiated directly.
Given: `class A { int x = 5; }` and `class B extends A { int x = 10; }`, what does `((A) new B()).x` evaluate to?
Answer: 5
Field access is resolved at compile time based on the reference type, so `((A) b).x` gives A's field value 5.
Which of the following correctly describes a covariant return type in Java?
Answer: An overriding method can return a subtype of the original return type
Java allows covariant return types, meaning an overriding method may return a subtype of the declared return type.
What is the result of calling a `default` interface method when a class implements two interfaces that define the same default method?
Answer: Compilation error unless the class overrides the method
If two interfaces provide the same default method, the implementing class must override it or a compilation error occurs.
Which access modifier makes a member visible only within the same package and to subclasses in other packages?
Answer: protected
`protected` grants access within the same package and to subclasses regardless of package.
What is the effect of marking a constructor `private` in a class?
Answer: The class cannot have any subclasses and cannot be instantiated outside the class
A private constructor prevents both external instantiation and subclassing, since subclass constructors must call a superclass constructor.