• 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

The java.lang.Object class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Seth Hutcherson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and well done getting to understand it
reply
    Bookmark Topic Watch Topic
  • New Topic