SCJP 1.4
All things are lawful, but not all things are profitable.
SCJP 1.4
Nor does the javac tool. It will think maybe an Animal is a Dog, Cat or Lion rather than a Camel. Try that code and see whether it will even compile.Urs Waefler wrote:. . . I do not understand the following code:
SCJP 1.4
SCJP 1.4
All things are lawful, but not all things are profitable.
SCJP 1.4
Urs Waefler wrote:Probably I asked the wrong question. It seems to be a theme of polymorphisme.
Animal animal = new Camel();
animal is a reference of the type Animal to an object of the class Camel. I am not sure about the use of this object; I think you can apply the methods of the class Animal only.
SCJP 1.4
You are taking a generic animal object and telling the compiler it is a camel. When you attempt to execute that cast at runtime, the runtime wil see that you don't have a camel object at all and will throw an exception.Urs Waefler wrote:. . . How do you explain this code?