The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Rajdeep Biswas wrote:
In an inheritance hierarchy, the parent types can hold child objects implicitly (why?), however for the child references to hold a parent object, explicit downcast is necessary (why?).
Good point. Lots of casts mean either a badly‑designed inheritance hierarchy or poor understanding of object‑oriented programming.Junilu Lacar wrote:. . . bad programming and design practice.
That might be a bit unfair on the compiler. You have surely written a compiler yourself and will know how difficult it is to keep track of types at the best of times. Think how much more difficult it would be to follow all the execution and verify that the cast will be correct. Especially if there is user input and the type will be impossible to predict. The javac tool has to be “trusting”, and believe that the programmers know what they are doing
In this regard, Java is like that forgetful fish, Dory, in the Finding Nemo movie. . . .
Because now it has all the information. It is no longer a case of believing that the cheque is in the post; it is a case of opening the envelope and seeing there is no cheque in it. The runtime does not “trust” because it can see everything, and knows that the cast was “fraudulent”. So you well deserve the class cast exception.However, the runtime system is not as trusting. . . .
Re-education, unless it is like something out of a Solzhenitsyn book, is too kind for them.. . . programmers who write this kind of code should be "reeducated". . . .
If you start adding methods to a class, as you are doing with Animal→Cat and Animal→Dog, please consider whether the inheritance is correctly designed or not.The JavaWorld article I cited above does give some good advice about downcasting though: Prefer polymorphism and dynamic binding over instanceof and downcasting.
Agree. That is a really good example of how not to do it. What they should have done is this:-That is polymorphism for youJunilu Lacar wrote:. . . purports to give an example of a legitimate use of downcasting but the code example given is still a poor one, . . .
Campbell Ritchie wrote:
That might be a bit unfair on the compiler. You have surely written a compiler yourself and will know how difficult it is to keep track of types at the best of times.Junilu Lacar wrote:In this regard, Java is like that forgetful fish, Dory, in the Finding Nemo movie
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Rajdeep Biswas wrote:If that's the case then why has Java allowed Animal to hold subtypes since there might be behavior typical to subtypes like a Dog will bark(), for that again compiler has to check and report. I know the rules, just trying to reason out in the simplest of sense.
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in