File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes The java.lang.Object class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The java.lang.Object class" Watch "The java.lang.Object class" New topic
Author

The java.lang.Object class

Seth Hutcherson
Greenhorn

Joined: Sep 07, 2011
Posts: 3
I am curious about how extending the java.lang.Object class works. You can only extend one class and by default every class extends the java.lang.Object class right? What happens when you try to extend another class other than Object. Does it just remove the extends for the Object class and replace it with whatever your extending (roughly)? Thank you for your help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18648
    
    8

No, it's the other way around. If you don't specify that your class extends some other class, then by default it extends the Object class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
I would suggest you write some classes and view their bytecode.
javac Bazz.java
javap Bazz
javap -c Bazz
. . . etc

Then you can see for yourself. You will also see what the compiler adds depending on which class you are extending.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
. . . and welcome to the Ranch
Seth Hutcherson
Greenhorn

Joined: Sep 07, 2011
Posts: 3
That makes more sense.

So say I have two classes, ClassA and ClassB where ClassA does not specify a superclass but ClassB extends ClassA.

public class ClassA { ... }

public class ClassB extends ClassA { ... }

Suppose I have a method that accepts an Object reference as a parameter,

public void doThis(Object obj) { ... }

If i send it an instantiated object of type Class B it will accept it because it inherits from ClassA which did not specify a superclass and therefore inherits from the Object class?

Also are the only type of methods that can be called on obj inside the method doThis() the ones inherited from the Object class?

Thank you very much for the welcome to the Ranch!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18648
    
    8

Yes, that's right. For every class there's a chain of inheritance which starts at the class and recursively goes through the parent classes until it reaches Object.

And yes, when you have an Object reference you can only call methods of the Object class. That's true of every type, in fact; when you have a T reference you can only call methods of the T type.

(I said "type" instead of "class" there because besides classes there are also interfaces, and "type" covers both of those things. I expect you haven't got to interfaces yet so don't worry about that and just pretend I said "class" there.)
Seth Hutcherson
Greenhorn

Joined: Sep 07, 2011
Posts: 3
I have done interfaces so thank you for letting me know it covers them as well. It has been a while since I have done Java and I am refreshing for my Data Structures class that uses Java for the implementations. Thank you for all the great responses!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
. . . and well done getting to understand it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The java.lang.Object class