*
The moose likes Beginning Java and the fly likes please help me out in casting of reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help me out in casting of reference variable" Watch "please help me out in casting of reference variable" New topic
Author

please help me out in casting of reference variable

shankar dengi
Greenhorn

Joined: Aug 09, 2008
Posts: 10
Can we cast subclass object to super class object?
Example:
class Animal{}
class Dog extends Animal{}
class demo
{
public static void main(string[] a)
{
Animal animal=new dog();
dog d=(animal)animal;
}
}
if this works how it possible because once we refer to reference variable we cant refer to other type ?
Schumi Mic
Greenhorn

Joined: Apr 17, 2010
Posts: 4

The above will not work, you cannot convert from animal to dog, dog is smaller and cannot hold animal object, for it to work, you have to type-cast it to Dog:


SCJP 1.5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

Casting doesn't change the actual object type. Casting simply says to the compiler "Yes, I know the reference is for THIS type, but I know the actual object is THAT type, so I can use a reference to THAT type ".

The proper way to do this is




The object you create is really a Dog. You initially set an Animal reference to point to it, which is OK because a Dog IS-A Animal.

Now you want a Dog reference to the object. You have to tell the compiler that the thing the Animal reference points to IS-A dog.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
shankar dengi
Greenhorn

Joined: Aug 09, 2008
Posts: 10
java is strictly typed language then how can we refer one type reference to another type object?
example:
class animal{}
class dog extends Animal{}
class demo
{
Animal animal=new dog();// how can we refer animal reference to dog object?
}


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

shankar dengi wrote:java is strictly typed language then how can we refer one type reference to another type object?
example:
class animal{}
class dog extends Animal{}
class demo
{
Animal animal=new dog();// how can we refer animal reference to dog object?
}

strictly typed? you mean strongly typed ?
Yes, java throw an exception when you typecast an object to an incompatible type at runtime[here dog can be campatible with Animal, since dog is an Animal]. but actually, here java uses static type, means compiler check the reference type i.e,the compiler checks that you don't assign incompatible types and if you assign incompitable type, hence error.

so, Animal animal=new dog();// this is ok
dog aDog =new Animal ();// you get compiler error, since java is a static typed language

hth
shankar dengi
Greenhorn

Joined: Aug 09, 2008
Posts: 10
Seetharaman Venkatasamy wrote:
shankar dengi wrote:java is strictly typed language then how can we refer one type reference to another type object?
example:
class animal{}
class dog extends Animal{}
class demo
{
Animal animal=new dog();// how can we refer animal reference to dog object?
}

strictly typed? you mean strongly typed ?
Yes, java throw an exception when you typecast an object to an incompatible type at runtime[here dog can be campatible with Animal, since dog is an Animal]. but actually, here java uses static type, means compiler check the reference type i.e,the compiler checks that you don't assign incompatible types and if you assign incompitable type, hence error.

so, Animal animal=new dog();// this is ok
dog aDog =new Animal ();// you get compiler error, since java is a static typed language

hth
static type means?
dog aDog =new Animal ();//why compiler will throw error aDog reference can point to animal object its subclass of animal then why ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

aDog reference can point to animal object

No, it can't. If you have a Dog reference, and you point it to an Animal object, what happens when you call the fetch() method? It is not defined in the Animal class, so bad things would happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help me out in casting of reference variable