Solved

In Java, why the compilation fails if we use reference variable of type Object?

Posted on 2011-02-19
5
1,001 Views
Last Modified: 2013-12-15
public class HorseTest
{
      public static void main(String [] args)
      {
            class Horse
            {
                  public String name;
                  public Horse (String s)
                  {
                        name = s;
                  }
            }
            
            Object obj = new Horse("Zippo");
            System.out.println(obj.name);
      }
}

Why compilation error occurs in this line "Object obj = new Horse("Zippo");"? I already know if I use Horse obj = new Horse("Zippo"), then it will be fixed. I just want clear explanation why using reference variable of type Object will cause problems?

Thanks in Advance :)
0
Comment
Question by:techbro
  • 5
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34935036
In this case it fails on the println operator because you use
vriable name outside of cass Horse
but Object obj = new Horse(name); does not
fail
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34935041
No sorry, that was wron sateemnt - it really fails on println, but...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34935051
Yes, if fails on println beacuse iit cannoy itrepret obj.name
You need first to cast obj to Horse
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34935056
this would compile:


public class HorseTest
{
      public static void main(String [] args)
      {
            class Horse
            {
                  public String name;
                  public Horse (String s)
                  {
                        name = s;
                  }
            }

            Object obj = new Horse("Zippo");
            System.out.println(((Horse)obj).name);
      }
}
0
 
LVL 47

Accepted Solution

by:
for_yan earned 50 total points
ID: 34935072
Variable obj belongs to class Object, and the latter does not
have variable "name", it is only class Horse has variable "name".
Therefore when compiler encounters obj.name it is puzzled - you first need
to cast obj to class Horse and then you can use its variables.

And because you created obj as an instance of Horse cast will work,
of obj would be instance of some unrelated class, then the actual casting operation
(Horse)obj would have raised cast Exception
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now