• 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

Question on casting

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InstanceTest
{
public static void main(String arg[])
{
InstanceTest i=new InstanceTest();
i.myMethod(i);
}

public void display()
{
System.out.println("Hello");
}

void myMethod(Object o)
{
System.out.println(o); //line 1
((InstanceTest)o).display();//line 2
//o.display();//line 3
}

}

My Question:
line 1 prints, InstanceTest@....
then why can't we invoke methods in the InstanceTest class directly with o. Why should we cast it with InstanceTest? Hope my Question is clear. Can anyboby clarify this?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference is of type Object and Object does not have any such method as display(). Seeing the reference type, neither the JVM nor the compiler can determine that the object that has been passed is indeed of type InstanceTest, so it can not automatically convert it to InstanceTest and call display().
The cast is required to make the compiler happy!
If this was not the case, then no one will ever create any variable other than type Object and boy it would have been a nightmare for people who read and write code
 
cm lak
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,I agree there is no method called "display()" in Object. But my Question is i.myMethod(i);, in this line we pass the reference variable. But here,
void myMethod(Object o){
System.out.println(o); //line 1
((InstanceTest)o).display();//line 2
//o.display();//line 3
}


when I try to print o, why is it printing "InstanceTest@..." instead of "java.lang.Object@....."

I am missing something...pls clarify...Hope my Question is clear...
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by cm lak:

when I try to print o, why is it printing "InstanceTest@..." instead of "java.lang.Object@....."


Okie, the reference is of type object and the instance is of type InstanceTest. In java, you can make a base class variable refer to a child class instance. At runtime, when a method on the reference is called, the call actually goes to the implementation in the actual instance(ploymorphism). toString() is the method called when you print any object. toString() is by default defined to print the object definition in the format you have specified.
May be this tutorial will explain things better.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic