• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Object class

 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have a basic doubt.Kindly look at the code below.


public class ObjRefTest {
public void test()
{
System.out.println("Sairam");
}
}

public class Call{
public static void main(String[] args) {
ObjRefTest t = new ObjRefTest();
t.test();

Object o = t;

System.out.println(t);
System.out.println(o);
//o.test();//This statement gives error "The method Test() is undefined for the type Object

}
}

We are assigning the reference of class ObjRefTest to the Object o.So why is the code giving error???

Kindly help me out.

Regards,
Sathya
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sathya Narayanan:
i have a basic doubt.


Yes, absolutely; this is indeed a basic question. So why have you posted it in "Java in General (Advanced)?" I'm going to move this to "Java in General (Beginner)", and then answer it.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here we are in the Beginner forum.

Every reference variable (a variable that points to any kind of object) can be said to have two types associated with it: the static type, or "compile-time type" which is the type you name when you declare the variable; and the dynamic type, or "runtime type", which is the type of the object the variable actually refers to when the program runs.

The Java compiler only knows the static type of a variable. Because you declared o as being of type Object, the compiler will only let you call methods declared in Object on it, even though you and I both know that o actually points to a ObjRefTest at runtime. You can call methods declared in ObjRefTest on o only by casting t to the proper type:

((ObjRefTest) o).test();

This tells the compiler that, despite the declared type of o, you know o will hold an instance of ObjRefTest when the program runs. The JVM will check this and throw a ClassCastException if you are wrong.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help spread the word: The word doubt is inappropriate and confusing for subject lines. I have no idea what you mean by doubt. Did you mean confusion? Even confusion, though, is too vague. The more meaningful & specific your subject, the more meaningful & specific the replies will be.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Richards:
Please help spread the word: The word doubt is inappropriate and confusing for subject lines. I have no idea what you mean by doubt. Did you mean confusion? Even confusion, though, is too vague. The more meaningful & specific your subject, the more meaningful & specific the replies will be.


If someone writes that they have just one doubt, I usually assume they have a lakh or a crore of them
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For non-Indians, it's useful to know that many Indians tend to use "doubt" to mean "question".

For Indians, it's useful to know that for non-Indians the word "doubt" implies other things which you probably don't mean. It sounds like you're disagreeing with what someone told you. Or maybe you're just insecure. If you use the word "question" instead, you will probably communicate better to the rest of the world.

For everyone, additional discussion of this point would be better off in another forum such as either Meaningless Drivel or JavaRanch. Let's not further hijack a Java discussion. Start a new thread if this interests you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Richards:
I have no idea what you mean by doubt.


Not to drag this off-topic, but...

This is more or less a colloquial expression in Indian English. "I have a doubt" means "I have a question." It's not a matter of incomplete or imperfect knowledge of English; in India, it's perfectly correct and understood, just as in Britain you can ask for a fag and be understood.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic