Non-Generic Method Overriding in Java Generics
Find Innovative SolutionsGenerate Solutions
Solution Overview
Problem
Existing Java programs with non-generic type declarations face compatibility issues when transitioning to Java libraries that use generic types, and non-generic subclasses may not be able to override methods with generic parameters due to differences in parameter types and return types.
Innovation Solution
Allow method overriding if the signatures of the putative overriding method and the overridden method are the same, or if the erasure of the signature of the overridden method matches the signature of the overriding method, and if the return types are either identical or return-type-substitutable, enabling non-generic methods to override generified methods without compilation errors.
Engineering Contradictions & Design Principles
Engineering Contradiction Analysis
1Manufacturing precision
If Java libraries transition to use generic types, then type safety and compilation precision are improved, but compatibility with existing non-generic programs deteriorates
Solution Approach 1:
The patent applies parameter changes by modifying the method signature parameters from generic types to raw types. When a subclass method overrides a superclass generic method, the subclass method uses raw type parameters (e.g., List instead of List<T>) while the superclass retains generic type parameters. This parameter transformation allows the subclass to compile without errors while maintaining the generic type safety in the superclass, resolving the contradiction between compilation precision and backward compatibility.
2Ease of operation
If non-generic subclasses are allowed to override generic superclass methods, then ease of operation is improved, but type safety may deteriorate
Solution Approach 1:
The patent applies local quality by allowing different type safety levels at different levels of the inheritance hierarchy. The superclass maintains strict generic type safety with parameterized types (List<T>), while the subclass is permitted to use raw types (List) for overriding methods. This localized differentiation in type quality enables ease of operation in subclasses without completely compromising type safety in the superclass, as the generic type constraints still apply to the overridden method's contract.
Data Source
AI summary
In accordance with one embodiment of the present invention, there is provided a technique for allowing a first method to override a second method as long as specified criteria are satisfied, even if the types of the parameters of the first method differ from the types of the parameters of the second method, and even if the return type of the first method differs from the return type of the second method. As a result of the technique, older methods, which do not have generic return types and/or generically typed parameters, can continue to override methods that did not used to have generic return types and/or generically typed parameters, but now do.


