This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes inplicit and explicit casting. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inplicit and explicit casting." Watch "inplicit and explicit casting." New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inplicit and explicit casting.