Solved

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

Posted on 2011-02-19
5
1,017 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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