Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inplicit and explicit casting.  RSS feed

 
saikrishna krishna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 15985
86
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saikrishna krishna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your fast reply.

i am still at scratch with core java. for my scjp065 xam.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!