Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Doubt in Object class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Object class" Watch "Doubt in Object class" New topic
Author

Doubt in Object class

Sat Nar
Ranch Hand

Joined: Oct 22, 2004
Posts: 83
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Nov 03, 2005
Posts: 135
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

Joined: Sep 16, 2005
Posts: 1780
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


There is no emoticon for what I am feeling!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jan 30, 2000
Posts: 18671
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Object class