• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Redecleration of Object class.

 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic