oop
21. Record Classes: Finality & Inheritance
Analyze the code, write your predicted output, and then reveal the solution.
The Scenario
Record classes were introduced to model plain data carriers. However, they have very strict rules regarding inheritance to ensure their "immutability" and "transparency" are preserved.
Predict what happens when we try to make a Record extend a class, or a class extend a Record.
class Base { void show() { System.out.println("Base"); } } // Case 1: Record extending a class // record Person(String name) extends Base { } // Case 2: Class extending a record record User(String id) { } // class Admin extends User { } public class Main { public static void main(String[] args) { User u = new User("123"); System.out.println(u.id()); System.out.println(u instanceof Record); } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.