Solved

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

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

617 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