1Z0-771 study materials
1Z0-771 study resources

1Z0-771: Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

1Z0-771 · Work through your exam, one topic at a time.

All guide sections

Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

Domain 3 tests encapsulation, inheritance mechanics, interface rules and polymorphism: what compiles, what is overridden versus hidden, and the order in which constructors and initializers run.

Classes and access

  • Access order: private < package-private (no modifier) < protected < public. protected also grants subclass access across packages, but only through a reference of the subclass type.
  • static members belong to the class and are hidden, not overridden. final prevents reassignment, overriding or subclassing depending on where it appears.
  • Initialization order: superclass static blocks, subclass static blocks, superclass instance initializers and constructor, then subclass instance initializers and constructor.

Inheritance

  • Every constructor starts with super(...) or this(...); the compiler inserts super() only when neither is written.
  • Overriding requires the same signature, no more restrictive access, a covariant return type, and no new checked exceptions.
  • Object contracts: equals and hashCode must agree; overriding one without the other breaks hash-based collections.

Interfaces and records

  • Interfaces may declare abstract, default, static and (since Java 9) private methods; fields are implicitly public static final.
  • A class inheriting conflicting default methods must override and can delegate with Interface.super.method().
  • Records (Java 16+, outside Java 11 scope) are implicitly final, extend java.lang.Record, and generate a canonical constructor, accessors, equals, hashCode and toString from their components.

Worked example

class A { A() { print(); } void print() { System.out.print("A"); } }
class B extends A { int x = 7; void print() { System.out.print(x); } }
new B(); // prints 0, not 7

A() executes before B's field initializers, so the overridden print() sees x at its default value.

Traps

  • Abstract classes may declare constructors; interfaces may not.
  • You cannot reduce visibility when overriding.
  • static methods are not polymorphic; calling one through an instance uses the declared type.