• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class Object

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was doing some experimenting and found that the following code compiles and runs without any exceptions or errors. But im not sure why.

01 final class Object {
02 Object() {
03 super();
04 }
05 }
06
07 public class ObjectTest {
08 public static void main (String [] args) {
09 new Object();
10 }
11 }

I have the following questions about this:
a) In line "01", how can you declare class Object final, since all other classes implicitly extend it.
b) In line "03", what does the call to super() do, since Object has no superclass?

Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a) In line "01", how can you declare class Object final, since all other classes implicitly extend it.



All other classes extend the Object class, in the java.lang package. You class is a different Object class, that is not in the java.lang package.

b) In line "03", what does the call to super() do, since Object has no superclass?



The super() calls the constructor to the class that your Object class is inheriting from -- the java.lang.Object class.

Henry
 
Ali Sadiq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry!

Now, if i add the line:
import java.lang.*;
to the top of my file, it still compiles and runs OK.

However, if i use:
import java.lang.Object;
only then do i get a conflict. Why is this?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Sadiq:

Now, if i add the line:
import java.lang.*;
to the top of my file, it still compiles and runs OK.

However, if i use:
import java.lang.Object;
only then do i get a conflict. Why is this?



Because that's how it works. If you import a specific class by name, then any unqualified use of that name always refers to the imported class. If you import a whole package with a wildcard, then locally-defined names (like your Object class) take precedence.

Note that defining a class called Object -- or indeed, a class with the same name as any class in java.lang or java.util -- is a terrible idea.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic