This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help me out in casting of reference variable

 
shankar dengi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shankar dengi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic