• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Variable Casting

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider code:
(this code is from K&b Guide Book)

class Animal
{

}
class Dog extends Animal{}
class DogTest
{
public static void main(String args[])
{
Animal animal =new Animal();
Dog d=(Dog)animal;
}
}

Why code gives a Runtime Error
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each object has two types: 1) The type of its reference variable, and 2) its actual type.

In the code example that you gave, the object reference variable, "animal" refers to an object of type "Animal". You cannot cast this object to a Dog type because its actual type is Animal.

The object referenced by the variable "animal" has two types: 1) Object and 2) Animal.

The error is not caught at compile-time because some Animal objects are of type Dog. In general, the compiler checks line-by-line and tries to find as many errors as it can at compile-time but the compiler doesn't do any computations so some errors show up at runtime and an exception or an error is thrown at runtime instead of a compile-time error occurring.
[ September 12, 2007: Message edited by: Kaydell Leavitt ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dog d = new Dog();
<left> <right>

Leftside is checked at Compile Time.
Rigthside checked at Runtime .......

..??
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!


Leftside is checked at Compile Time.
Rigthside checked at Runtime .......

..??


Not quite. The variable gets a type once and for all to know what methods or fields can be used on objects of the type.
But what the object really is (the right side) can only partially be checked by the compiler.

The compiler only looks, if the type hierarchy is ok, e.g. it won't let you assign a String to an Animal variable.

But when these are ok, the rest happens at runtime, because you can easily assign subclasses to yor variable. Or implementers if the variable has got an Interface type.

Here's an example for assigning objects of different subtype at runtime, for those who are bored of animals.
:


At compile time the compiler does not know exactly what subtype will be assigned as element of the array. Compiler only checks, if it really is a subtype of Planta (or Planta itself). But the object type will be known only at runtime.

output e.g. like
I am a Bougainvillea
I am a Planta
I am a Planta
I am a Conium and I'm poisonous
I am a Magnolia
I am a Bougainvillea
I am a Magnolia
I am a Bougainvillea
I am a Bougainvillea
I am a Bougainvillea



Yours,
Bu.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaydell Leavitt:
Each object has two types: 1) The type of its reference variable, and 2) its actual type...


Hmmm... Be careful with this wording.

An object has a true runtime type, determined by what constructor was used with "new." It also has supertypes (unless the true runtime type happens to be the base class, java.lang.Object). In this sense, an object can have many types.

A reference also has a type, which must be the type of the object it's pointing to, or any of that object's supertypes.

So rather than talking about the "object" having "two" types, I think it's important to distinguish between the reference type and the object's true runtime type.
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic