Solved

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

Posted on 2011-02-19
5
1,007 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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