Liskov Substitution Principle and Return Type Variance

Whenever you create a subclass in Java you can override the inherited methods. You can even vary the the return types. But you are limited when doing so, because your subclass will have to satisfy the Liskov Substitution Principle (LSP). In this blog post I will discuss in which respect you are allowed to change the return type.

Continue reading Liskov Substitution Principle and Return Type Variance

Double Dispatch, the Visitor Pattern and the Expression Problem in Java

By design Java and C++ only offer single dispatch on method calls. Meaning that the dispatch is only based on the dynamic data type of the receiver. This becomes a problem if you want to have different method implementation based on the dynamic type of the argument and the receiver. In this post I am going to show how you circumvent the limitation using the double dispatch technique in Java. One of the more apparent usages of the double dispatch is in the famous Visitor Pattern. The discussion will then lead to the Expression Problem.

Continue reading Double Dispatch, the Visitor Pattern and the Expression Problem in Java