Two Laptop Bag*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 14074
    
  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.
 
 
subject: inplicit and explicit casting.
 
Similar Threads
find the error...
Question on Inheritance
overriding
Overcome hiding method for static--pls help
does static methods inherit ? what about private ?