• 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

How does every Class extends Object???

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I write a java code,i could find that the methods available in Object class are visible.
So my class extends Object implicitly but i want to know who is doing this?
is it Complier or is it classloader?
could any one explain me?

thanks in advance,
Dhaya
 
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Try this

javac Foo.java
javap -c Foo

What you see is what the compilation has done: you haven't gone anywhere near a class loader yet.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dhaya nidhi wrote:is it Complier or is it classloader?
could any one explain me?



In some sense it's shared work.

If you do,

class Myclass {}

the compiler treats it as if it were,

class Myclass extends Object {}

And in fact you can actually extend Object explicitly like above if you want to.

Then at runtime a classloader loads the actual Object class (together with lots of other "system" classes one hasn't even heard about).
 
Ranch Hand
Posts: 188
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

uj nossnahoj wrote:

the compiler treats it as if it were,

class Myclass extends Object {}

And in fact you can actually extend Object explicitly like above if you want to.



implicitly or explicitly, if Object is extended, how we are able to extend one more class? Say class A extends B{ } how it is possible that A also extends Object implicitly?
 
Rancher
Posts: 600
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

implicitly or explicitly, if Object is extended, how we are able to extend one more class? Say class A extends B{ } how it is possible that A also extends Object implicitly?



It doesn't. A class can only have one superclass. If one is not specified, then it is implicitly set to Object. For example, if class C extends class B, and class B extends class A, and A implicitly extends Object, only class A's superclass is Object. Object is still on all classes' hierarchies, but only A directly extends it.

John.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul.p Kumar wrote:implicitly or explicitly, if Object is extended, how we are able to extend one more class? Say class A extends B{ } how it is possible that A also extends Object implicitly?



Object isn't really the superclass of all classes. It's the top superclass of all class hierarchies.

So if you do,

class A {}

this is interpreted as

class A extends Objects {}

But if you do,

class B {}
class A extends B {}

then this is interpreted as,

class B extends Object {}
class A extends B {}

As you can see Object is the top superclass of the hierarchy rather than the superclass of every class. But it's also worth noting that the methods of Object are inherited all the way down to the bottom subclass, even though they may be overridden along the way.

 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tingeling.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul.p Kumar wrote:Thanks Tingeling.

Michele too.
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic