Inheritance in Java

Inheritance is a core concept in object-oriented programming (OOP) that allows a class, known as a subclass, to inherit fields and methods from another class, known as a superclass. This mechanism promotes code reuse and establishes a natural hierarchy among classes, making it a fundamental aspect of Java programming.

Inheritance in Java Persistence

In the context of Java persistence, particularly when using frameworks like Spring Data and Hibernate, inheritance poses unique challenges. SQL databases do not natively support table or data type inheritance, which complicates the representation of polymorphic associations and queries. To address these challenges, Object-Relational Mapping (ORM) solutions like Hibernate offer various strategies to map class hierarchies to database tables.

Inheritance Strategies

Table Per Class Hierarchy

The table per class hierarchy strategy maps an entire class hierarchy to a single table. This approach includes columns for all properties of all classes in the hierarchy, along with an extra type discriminator column to identify the concrete subclass represented by a particular row. While this strategy is known for its performance and simplicity, it can lead to data integrity issues due to nullable columns and denormalization.

Table Per Subclass with Joins

The table-per-subclass with joins strategy represents inheritance relationships as SQL foreign key associations. Each class or subclass with persistent properties has its own table. The subclass table contains columns for non-inherited properties and a primary key that is also a foreign key of the superclass table. This strategy normalizes the SQL schema and simplifies schema evolution and integrity-constraint definition, making it a robust choice for complex inheritance structures.

Choosing an Inheritance-Mapping Strategy

Selecting the appropriate inheritance-mapping strategy depends on several factors, including the frequency of querying for superclass instances and the attributes of super- and subtypes. For simple cases, InheritanceType.SINGLE_TABLE is advisable, while InheritanceType.JOINED is better for complex cases or when NOT NULL constraints are important.

Example: E-commerce Application

In an e-commerce application, inheritance is used to handle different billing strategies. The BillingDetails superclass has multiple concrete subclasses: CreditCard and BankAccount. Each subclass defines different data and functionality. This model is illustrated in the UML class diagram below:

Using inheritance for different billing strategies Figure 1.3 Using inheritance for different billing strategies

This diagram demonstrates how inheritance can be effectively used to manage different billing strategies within an application, showcasing the flexibility and power of using inheritance in object-oriented design.

Inheritance in Kotlin

In Kotlin, inheritance is expressed using the : syntax, differing from Java's extends keyword. Kotlin's approach to inheritance emphasizes safety and predictability.

Key Characteristics of Inheritance in Kotlin

  1. Closed Classes by Default: Classes in Kotlin are closed (or final) by default, meaning they cannot be subclassed unless explicitly marked with the open keyword. This design choice encourages developers to explicitly declare their intention to allow inheritance.

  2. Open Methods for Overriding: Methods in a class are also closed by default. To allow a method to be overridden in a subclass, it must be marked with the open keyword. This ensures that only methods explicitly designed to be overridden can be changed in subclasses.

Syntax Example

Here is a simple example to illustrate inheritance in Kotlin:

open class ParentClass {
    open fun display() {
        println("This is the parent class method.")
    }
}

class ChildClass : ParentClass() {
    override fun display() {
        println("This is the child class method.")
    }
}

In this example, ParentClass is marked as open, allowing it to be subclassed by ChildClass. The display method in ParentClass is also marked as open, permitting ChildClass to override it. The override keyword is used in ChildClass to provide a new implementation of the display method.

By understanding and utilizing these features, developers can effectively manage inheritance in Kotlin, ensuring that their code is both flexible and robust.

For more detailed information, refer to the Java Persistence with Spring Data and Hibernate and The Well-Grounded Java Developer, Second Edition.

| Book Title | Usage of Inheritance | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|----------------------|-----------------|-------------------------------|---------------|-----------------------|
| [Java Persistence with Spring Data and Hibernate](https://www.manning.com/books/java-persistence-with-spring-data-and-hibernate) | Discusses inheritance in the context of ORM and SQL databases, focusing on challenges and strategies for mapping class hierarchies. [more](https://livebook.manning.com/book/java-persistence-with-spring-data-and-hibernate/chapter-1/63) | Explores various inheritance mapping strategies like table per class hierarchy and table per subclass with joins. [more](https://livebook.manning.com/book/java-persistence-with-spring-data-and-hibernate/chapter-1/65) | Connects inheritance to ORM strategies and SQL schema design, emphasizing the impact on data integrity and performance. [more](https://livebook.manning.com/book/java-persistence-with-spring-data-and-hibernate/chapter-7/161) | Provides an example of an e-commerce application using inheritance for billing strategies. [more](https://livebook.manning.com/book/java-persistence-with-spring-data-and-hibernate/chapter-1/47) | Focuses on practical ORM solutions for handling inheritance in database applications. [more](https://livebook.manning.com/book/java-persistence-with-spring-data-and-hibernate/chapter-1/63) |
| [The Well-Grounded Java Developer, Second Edition](https://www.manning.com/books/the-well-grounded-java-developer-second-edition) | Covers inheritance in Kotlin, highlighting differences from Java, such as the use of `:` instead of `extends`. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-9/129) | Discusses Kotlin's approach to inheritance with closed classes and open methods, promoting safer code. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-9/129) | Links inheritance to Kotlin's design philosophy of explicit subclassing and method overriding. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-9/129) | Provides a syntax example demonstrating inheritance in Kotlin with open classes and methods. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-9/129) | Emphasizes the importance of explicit inheritance in Kotlin for robust and predictable code. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-9/129) |

FAQ (Frequently asked questions)

How does SQL handle polymorphic associations and queries?

What solutions exist for mapping class hierarchies to database tables?

What is Figure 1.3 about in the context of inheritance?

What does each subclass in the e-commerce application define?

Which inheritance-mapping strategy is recommended for polymorphic associations or queries with few subclass properties?

What is the default inheritance-mapping strategy for simple cases?

What must be done to methods in Kotlin to allow them to be overridden?