| Author |
inplicit and explicit casting.
|
saikrishna krishna
Greenhorn
Joined: Jul 10, 2011
Posts: 2
|
|
The second class, a subclass of Animal, is called Cat:
ths program is 100& correct. but i had a dt in
no where in the above statement "new " keyword is used.
but for myAnimal object, myCat got assigned . how is it possible .can you xplain?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12909
|
|
Welcome to the Ranch!
It's important to keep in mind that inheritance means there is an "is a" relationship between the subclass and the superclass. So, a Cat is an Animal, because Cat is a subclass of Animal. You can assign a Cat to a variable of type Animal without explicit casting, because a Cat is an Animal - all methods that are available on Animal are also available on Cat.
Ofcourse, the other way around doesn't work. Suppose you would write this:
Because an Animal is not always a Cat.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
saikrishna krishna
Greenhorn
Joined: Jul 10, 2011
Posts: 2
|
|
Thank you for your fast reply.
i am still at scratch with core java. for my scjp065 xam.
|
 |
 |
|
|
subject: inplicit and explicit casting.
|
|
|