• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Redecleration of Object class.

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



But if we write in the import declaration

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

Regards,
Shivani,
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic