This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Redecleration of Object class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Redecleration of Object class." Watch "Redecleration of Object class." New topic
Author

Redecleration of Object class.

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,

Object class is the member of java.lang package which is implictly imported in every source file.

consider the following code:


This code compiles and runs fine.

I want to know that if Object class is already present in the current scope how are we able to redeclare it.

Also JLS states that :

The extends clause must not appear in the definition of the class Object, because it is the primordial class and has no direct superclass.


Then how are we able to extend the thread class in the above mentioned code?

Thanx

Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380



But if we write in the import declaration

This code will not compile. JLS has a good section on this ...

Regards,
Shivani,


/** Code speaks louder than words */
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
HI,

Doesnt java.lang package already imported in each and every source file.
and if it is then ofcourse all of its compilation units are also there.

then why writing :
import java.lang.Object;
gives error?


Sandy
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380

Doesnt java.lang package already imported in each and every source file.
and if it is then ofcourse all of its compilation units are also there.
then why writing : import java.lang.Object; gives error?


java,lang - It is imported explicitly but can be overriden with one's own class name . Explicitly writing an import java.lang.Object causes ambiguity - because the compiler does not know which <ClassName> to substitute with full package name [java.lang...] and which is is customized [myPackage.Object]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,

Thanx for your replies

This is an abstract from JLS Ch-7


A package consists of a number of compilation units (�7.3). A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.



According to this class java.lang.Object is automatically imporeted in our source file.
if i dont declare the statement:
import java.lang.Object;
even though it is being automatically imported in the source file
Then why there is not ambiguity in that case?

Sandy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Redecleration of Object class.
 
Similar Threads
How does every Class extends Object???
throw Exception
Thread Related Problem
Thread
Exercise 9-2(page 738)Sun certified programmer for Java6"Study Guide"