Thursday 20 May 2021

Java Inheritance for Beginners

Just a little exercise, to get things in perspective.

Besides, I thought I was being clever, but made a rookie mistake and it just didn't work1.

Something similar can be found in Java Puzzlers2.

During compile time a decision is made which method is to be called (§15.12)3. During compile time it is as yet unknown which type/subtype it will be. The compiler tries to be as specific as possible, but in this case, it will just be Animal in the second testcase.

References

[1] Stackoverflow - Overloaded method selection based on the parameter's real type
https://stackoverflow.com/questions/1572322/overloaded-method-selection-based-on-the-parameters-real-type
[2] Java Puzzlers - Traps, Pitfalls, and Corner Cases - By Joshua Bloch and Neal Gafter
http://www.javapuzzlers.com/
[3] JLS 16 - 15.12. Method Invocation Expressions
https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.12

No comments:

Post a Comment