OCP exam prep: inheritance¶
☕ Java 17 · 🎓 OCP 1Z0-829 · Building Classes on Top of Other Classes
Optional page
This page is for readers preparing the Oracle Certified Professional Java SE 17 Developer exam (1Z0-829). The concepts are all you need otherwise.
Exam objective: Utilizing Java Object-Oriented Approach — implement inheritance, including visibility modifiers.
Four traps to watch for¶
Trap 1 — final blocks children only
A final class can extend another class. Any class extending a final class doesn't compile.
Trap 2 — One name after extends
class X extends A, B never compiles. Only interfaces can be listed several at a time.
Trap 3 — private is not accessible
A child can't touch a parent's private field directly, even though the object still holds the data.
Trap 4 — Check the package lines
Members with no modifier reach a child only if both classes are in the same package. Read the first line of each file.
Practice questions¶
1. Each class is in its own file, same package. Which line does NOT compile?
1: public class Content { }
2: public final class Movie extends Content { }
3: public class Series extends Content { }
4: public class Trailer extends Movie { }
5: public final class Episode extends Series { }
A. Line 2
B. Line 3
C. Line 4
D. Line 5
Answer
C. Movie is final, so Trailer can't extend it. Lines 2 and 5 are fine: a final class may still have a parent.
2. Movie is in a different package. Which fields can it access directly?
A. title only
B. title and genre
C. views and title
D. All three
Answer
A. protected reaches subclasses in any package. genre has package access and the packages differ. views is private.
3. public class User { String email; } has no extends. Which TWO statements are true?
A. User implicitly extends Object
B. User has no parent class
C. user.toString() compiles
D. User can extend two classes
Answer
A and C. The compiler adds extends java.lang.Object, so User inherits toString(). Only Object has no parent, and no class has two direct parents.