abalfazl hossein wrote:These returns true, Then again: Why does it need to cast?
Casting is relevant when you have a variable with one reference type, which is referring to an object which is actually an instance of a subclass. For example:
All the compiler knows about animal is that is referencing an Animal. So as long as we only need to use features of an Animal, this is fine.
But what if we want to use a feature specific to Cat? The compiler doesn't know that it's a Cat. So it can't allow us to do this:
In this case, we need to tell the compiler "trust me - I know the reference type says Animal, but it's definitely containing a Cat". Normally the compiler protects us from accidentally treating a Dog like a Cat, so we need to override it. That's what casting does:
Then, if we're wrong, the run-time will throw a ClassCastException.
So, casting is needed when we need to tell the compiler at compile time that something will be true that it can't check until run time.
abalfazl hossein wrote:Is it possible one object has two kinds?! Because in that code pet has two kinds!
Yes, because that's what inheritance means. A Cat is an Animal. If something is a Cat, it must also be an Animal. But the other way around is not necessarily true, and that's why casting is sometimes necessary.
By "two", I presume you mean three.
abalfazl hossein wrote:Is it possible one object has two kinds?! Because in that code pet has two kinds! Again: Why does it need cast?. . . .
That makes two.
Matthew Brown wrote:A Cat is an Animal. If something is a Cat, it must also be an Animal.
But we are writing in Java™, so a Cat is an Animal and an Animal is an Object. That makes three
And some objects have more types; for example an instance of this class has 7 types.
abalfazl hossein wrote:
view plaincopy to clipboardprint?
Pet pet = new Cat(“Alley”, 7);
pet.eat(); //no cast needed
((Cat) pet).sleep(); //cast is needed
pet is an instance of a Cat,Because of new Cat,Then Why does it need to cast?
At compile time all the compiler knows is it is a Pet. A Pet does not know how to sleep, the method is specific to Cats. (Please don't quote this without context ;-)).
َand in Runtime, Pet is a Cat?
Whichever way it is you are adding new methods, it means that only certain subclasses have certain behaviour. For example, java.util.ArrayList has three methods which are not in the List interface: trimToSize, ensureCapacity and removeRange. If you need those, you would have to castSo when you want 10000000 numbers, you get a List large enough to hold them. Those methods (trimToSize and ensureCapacity) are not in LinkedList, because they are not necessary there.
The instanceof operator tests whether you have an ArrayList or one of its subclasses, so the cast can go ahead safely. I think that is an awkward programming construct, however, and it should be used only rarely. And it will be awkward if you find another type of List which also has an ensureCapacity() method or a trimToSize() method.
Another awkward construct there is the use of an output parameter; the List is passed in order to have its state changed. If you didn't cast the Pet object to (Cat)tiddles or similar before invoking the sleep() method, the compiler would only know it is a Pet and would not know there is an accessible sleep() method, so it would never compile. And I am surprised only Cat has a sleep() method; have you never seen a dog asleep?
When we use inheritance, Super class and Subclass has same type
By this order, test became a.
a.eat must print a is eating
Exception in thread "main" java.lang.ClassCastException: inheritancetest.a cannot be cast to inheritancetest.b
Subclass and superclass has same type.But it is not possible to cast subclass to super class, Because an a object may be a "b" object, But not necessarily.Right?
abalfazl hossein wrote: . . . But it is not possible to cast subclass to super class, . . ..Right?
It is always possible to cast from subclass to superclass; it is often not possible to cast from superclass to subclass.
And the casts vanish at runtime. Try this codeCompile that code and print the bytecode with javap -c AnimalDemo and you will see no sign of the cast. So the Cat object is still a Cat object.
Does it really? I don't think it does. Have you tried it?
abalfazl hossein wrote:
Yes, but . . .
It shows polymorphism too. Right?
What you have shown is very different from Ryan Beckett's code you quoted earlier. you should create as many objects as you need, and you should avoid casting as much as possible.
If you compare this code to another:http://www.coderanch.com/forums/posts/list/531088#2408422
Which one is better?Cast or create new instancne?
And please post code which compiles; your "smart" quotes won't compile. Use a text editor, not a word processor.