OP should have noted from "They have the Supreme Court on their side[,] which ruled in their favour" (in the linked question) that "which" can often apply to the entire preceding phrase.
OP's first example here is both clumsy phrasing and conceptually flawed. Per the above, the natural interpretation is the fact that Class Sheltie of Dog.scala illustrates variable shadowing is the reason the class does what it does. That can only make sense if we interpret it as meaning the class is specifically written for the purpose of illustrating variable shadowing.
But OP says the reason the class does what it does is because of variable shadowing, which I don't think that makes any sense at all. I'm stuck between a weird interpretation and a nonsense one.
The second example is natural English, and unambiguous. Although only the default constructor is referenced by which, not the whole of the preceding phrase, that's not a problem. It's the only plausible noun phrase, and we can easily see it's a suitable candidate for being the body of the class. It could be recast to put "which" closer to its referent, but that might seem a little more awkward...
This causes the default constructor, which happens to be the body of the class itself, to be invoked.
Note that if instead OP's second example had been...
This causes the default constructor to be invoked, which is undesirable.
...we're back with the "whole phrase" interpretation - it's not the default constructor which is undesirable, but the fact that something causes it to be invoked.