wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Object: the root class of every class in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object: the root class of every class in java?" Watch "Object: the root class of every class in java?" New topic
Author

Object: the root class of every class in java?

Castor Tack
Ranch Hand

Joined: Dec 06, 2010
Posts: 31
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.


The BEST user interface design I've ever seen? --- A flush toilet.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Castor Tack
Ranch Hand

Joined: Dec 06, 2010
Posts: 31
Got it. Thank you, buddies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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

    Joined: Oct 13, 2005
    Posts: 38509
        
      23
    Also, you didn't provide a constructor. Look at the printout and see what has happened about constructors.
    Castor Tack
    Ranch Hand

    Joined: Dec 06, 2010
    Posts: 31
    To Campbell:

    Impressive! It can't be clearer.
    Thanks.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38509
        
      23
    You're welcome
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Object: the root class of every class in java?