File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can we create Object class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can we create Object class?" Watch "Can we create Object class?" New topic
Author

Can we create Object class?

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
While attending some mock questions, i wondered to the code below

The answer says line 10 produce a compile time error. I verified the same. But how come line 19 works? Can we redefine Object class? If yes, what is the purpose? Or my understanding is wrong? If i comment out line number 10 the programes compiles and gives output "Raman" !!!

Thanks for any help!!


live
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Quite interesting question!
Well, I checked and not just Object; String etc.. too compile.
I think, because:
1. you are not violating any class declaration rules.
2. the rules never say that we are not allowed to use the class names that are already used by the Java library.
3. if you observe, you are not saying which package your-defined-Object class belongs. It just belongs the current directory, yeah? NOT java.lang package.
4. even if you include it in package java.lang still its ok. why? because, your package still resides in your current working directory, right?! and NOT in java.lang package.

I will key down if i find more interesting things! But, it was really never-ever-thought question. hehe..
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Check this..

Do not laugh at my crazy code!
Here is the result after javac.exe:
No compilation errors.
Here is the result after java.exe:

Hmm..
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No problem for me - compiles and prints "Object Class"


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Richard Parker
Ranch Hand

Joined: Jan 23, 2007
Posts: 70
Faisal -
Your code worked for me too.


Raja -
What a tricky question!
Line //A does work (for obvious reasons) if you put java.lang.Object.

It just goes to show - you that you can't be a sleepy-head during the exam!


"...it takes all the running you can do to keep in the same place. <br />If you want to get somewhere else, you must run at least twice as fast as that!" <br />~ Through the Looking-Glass
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
Originally posted by Faisal Pasha:
Quite interesting question!
Well, I checked and not just Object; String etc.. too compile.
I think, because:
1. you are not violating any class declaration rules.
2. the rules never say that we are not allowed to use the class names that are already used by the Java library.
3. if you observe, you are not saying which package your-defined-Object class belongs. It just belongs the current directory, yeah? NOT java.lang package.
4. even if you include it in package java.lang still its ok. why? because, your package still resides in your current working directory, right?! and NOT in java.lang package.

I will key down if i find more interesting things! But, it was really never-ever-thought question. hehe..


So can I conclude that my code is compiled because the Object I defined is different from the Object Sun defined?

Barry Gaunt, your opinion please!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by raja kanak:


So can I conclude that my code is compiled because the Object I defined is different from the Object Sun defined?

Barry Gaunt, your opinion please!


The class Object that you have defined in the default package is a (very) different one from java.lang.Object. But it is still derived from java.lang.Object. It is best to avoid such confusing practices. Your original code did not compile because the compiler was seeing your Object class instead of java.lang.Object. When you comment out line 10 then the compiler no longer needs to resolve the symbol Object so it compiled successfully.
[ February 19, 2007: Message edited by: Barry Gaunt ]
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
hmmm...I think this is a case of shadowing where compiler will consider your class instead of already defined classes.

This is a very good way to experiment things. I remember doing wierd stuffs and coming to conclusion with codes.


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can we create Object class?