Proper usage of 'which' in a sentence
Solution 1:
• Class Sheltie of Dog.scala illustrates variable shadowing, which also explains why this class does what it does.
The usage of which clouds the issue, in my opinion. To avoid confusion, you might rewrite this as:
Class Sheltie of Dog.scala exists to illustrate variable shadowing.
Regarding your second sentence:
• This causes the default constructor to be invoked, which happens to be the body of the class itself.
I believe this is a clear and proper use of which. You might say that the default constructor constitutes the body of the class as I get the feeling there are no other methods.
Solution 2:
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.