This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object: the root class of every class in java?

 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there:

I'm reading this line on Oracle's java tutorial (Oracle's Java Tutorial):
"Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. "

Does that mean, for example, when I am doing a simple project and write a class:


Java will automatically make my SimpleClass a sub-class of Object?

Or

The tutorial is simply saying that everything provided by Java (array, map ... etc) has Object as its super/root class?

Thanks for any input.
 
Paul Clapham
Sheriff
Posts: 21120
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Castor Tack wrote:Hi there:

I'm reading this line on Oracle's java tutorial (Oracle's Java Tutorial):
"Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. "

Does that mean, for example, when I am doing a simple project and write a class...

Java will automatically make my SimpleClass a sub-class of Object?

Yes, that's exactly what it means.
The tutorial is simply saying that everything provided by Java (array, map ... etc) has Object as its super/root class?

There isn't any difference. The class ArrayList was written by some people at the former Sun Microsystems company. The class SimpleClass was written by you. Both of them are "provided by Java" in the sense that they were written in Java and compiled by a Java compiler. There's nothing special about classes which are in the standard API except that some of them are in packages whose names begin with "java" or "javax", which you aren't allowed to do. Apart from that they are just ordinary Java code written by ordinary human beings.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference, see JLS - 4.3.2 The Class Object...

The class Object is a superclass (ยง8.1) of all other classes... All class and array types inherit the methods of class Object...
 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you, buddies.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Castor Tack wrote: . . . Does that mean, for example, when I am doing a simple project and write a class: . . . Java will automatically make my SimpleClass a sub-class of Object? . . .
Yes.

Try the following:
  • Create your simple class.
  • Compile with javac SimpleClass.java
  • View the bytecode with javap -c SimpleClass
  • See what the compiler has added to the name of the class.
  •  
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49382
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, you didn't provide a constructor. Look at the printout and see what has happened about constructors.
     
    Castor Tack
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To Campbell:

    Impressive! It can't be clearer.
    Thanks.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49382
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic