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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inplicit and explicit casting." Watch "inplicit and explicit casting." New topic

inplicit and explicit casting.

saikrishna krishna

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: 14996

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 8 API documentation
saikrishna krishna

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:
subject: inplicit and explicit casting.
jQuery in Action, 3rd edition