Java record inheritance example

Rachel 58 Published: 12/07/2024

Java record inheritance example

Here is an example of using records with inheritance in Java:

Java has introduced Records as a new feature in Java 14. Record is a class that can be used to hold the state of an object and provide getter methods for that state.

In the following example, we have two classes Student and Teacher. Both classes inherit from a common superclass called Person.

public record Person(String name) {

public String getName() {

return name;

}

}

public record Student(String name, int age, String department) extends Person {

public int getAge() {

return age;

}

public String getDepartment() {

return department;

}

}

public record Teacher(String name, int age, String subject) extends Person {

public int getAge() {

return age;

}

public String getSubject() {

return subject;

}

}

In the above example:

Person class is a record that takes one parameter - name. Both Student and Teacher classes inherit from Person. They extend Person using the extends keyword. Each of these classes also has their own unique data fields. For Student, it's age and department; for Teacher, it's subject. Since they both inherit from Person, they automatically get a default constructor, getters for name, and overrides of toString(), equals(), and hashCode().

Now let's use these records to create some instances:

public class Main {

public static void main(String[] args) {

Student student = new Student("John", 25, "Computer Science");

Teacher teacher = new Teacher("Jane", 30, "Mathematics");

System.out.println("Student: " + student);

System.out.println("Teacher: " + teacher);

// Getters can be used to access the data fields

System.out.println("Student's name is " + student.getName());

System.out.println("Student's age is " + student.getAge());

System.out.println("Student is in " + student.department());

System.out.println("Teacher's name is " + teacher.getName());

System.out.println("Teacher's age is " + teacher.getAge());

System.out.println("Teacher teaches " + teacher.subject());

}

}

In the Main class, we create instances of Student and Teacher, and then use their getters to access the data fields.

The output will be:

Student: Student[name=John, age=25, department=Computer Science]

Teacher: Teacher[name=Jane, age=30, subject=Mathematics]

Student's name is John

Student's age is 25

Student is in Computer Science

Teacher's name is Jane

Teacher's age is 30

Teacher teaches Mathematics

Java record inheritance interview questions

Here are some common Java record inheritance interview questions:

Q: What is the concept of record inheritance in Java?

A: Record inheritance in Java allows a subclass to inherit all the properties and behavior of its superclass, just like traditional class inheritance. However, it's specifically designed for records (value classes) and provides better type safety and ergonomics.

Q: How do you use record inheritance with Java 14+ records?

A: You can use the record keyword to define a subclass that inherits all the properties and behavior of its superclass. For example, if you have a record Person with attributes name, age, and address, you can create a subclass Student like this:

public record Student extends Person {

public final Optional studentId;

}

Q: What are the benefits of using record inheritance in Java?

A: Some benefits include:

Improved type safety, as the compiler checks that the subclass only adds or overrides methods and fields, but not removes them. Better ergonomics, as the subclass can inherit all the properties and behavior of its superclass without having to manually delegate calls to the superclass. Simplified code, as you don't need to write boilerplate code to delegate calls to the superclass.

Q: Can I override a record method in a subclass?

A: Yes, just like with traditional classes, you can override a record method in a subclass. For example:

public record Person {

public String getName() { return name; }

}

public record Student extends Person {

@Override

public String getName() { return super.getName() + " (student)"; }

}

Q: How do you handle multiple inheritance with Java records?

A: Java doesn't support multiple inheritance in the classical sense, but you can achieve similar effects using interfaces. For example:

public record Person {

public String getName() { return name; }

}

public interface Mammal {

public boolean isBreastFed();

}

public record Student extends Person implements Mammal {

@Override

public boolean isBreastFed() { return false; }

}

Q: What are some common pitfalls to avoid when using record inheritance in Java?

A: Some common pitfalls to avoid include:

Inadvertently overriding a superclass method without realizing it. Forgetting to use the super keyword when delegating calls to the superclass. Not carefully considering the implications of inheriting all the properties and behavior of the superclass.

Q: Can I use record inheritance with Java 8+ default methods?

A: Yes, you can use record inheritance with Java 8+ default methods. For example:

public record Person {

public default void sayHello() { System.out.println("Hello!"); }

}

public record Student extends Person {

@Override

public default void sayHello() { super.sayHello(); System.out.println("Goodbye!"); }

}

I hope these questions and answers give you a good sense of how to use record inheritance in Java and what kind of scenarios you might encounter when using it. Happy coding!