0

I have a query in java, that since Object class is the parent class for all, I want to know that is Object class is being implicitly extended by every class in java.

For example: if I make an object class B then I extend class A that is ok since in java, class can extend only a single class at most, but since my class B has implicitly extended the Object class then how can it extend class A? Please advise.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
user2080568
  • 93
  • 1
  • 2
  • 8
  • 2
    `B` extends `A` extends `Object` – Greg Kopff Feb 20 '13 at 12:47
  • 1
    `Object` will always be the lowest part in your class-hierarchy. – Lukas Knuth Feb 20 '13 at 12:47
  • Please consider the [FAQ](http://stackoverflow.com/faq) on how to ask questions here. When you extend a class in Java you implicitly extend the Object-class, too, as this is part of your base class. If you don't extend any class, but create a new one, you implicitly extend Object. – bash.d Feb 20 '13 at 12:48
  • class B extends A {} which implicitly extends Object – PbxMan Feb 20 '13 at 12:48

4 Answers4

0

Only when a parent is not specified does it implicitly inherit off of Object, otherwise it only inherits off of the specified parent.

Side note:

This is allowed:

  C
  |
  A
  |
  B

In the above case, replace C with Object to be in line with what you're asking.

This is not allowed:

C   A
 \ /
  B

The inheritance can be as deep as you want it to be, but one object cannot have multiple parents.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

Think of it as a family tree where individuals only have one parent. Each individual only has one parent, but all that parent's ancestors are also ancestors of the individual. That's what's meant when it is said that all classes extend Object: it is the most remote ancestor of all, though only the direct ancestor (parent) of those classes that are declared with extends Object or with no extends declaration at all.

entonio
  • 2,143
  • 1
  • 17
  • 27
0

I think you know the concept but you must be a bit confused. Think of a tree. Object class is the root of that tree. It has leaves and other nodes in between. No matter how your class hierarchy is designed, it will eventually an Object because its root is that. So you can extend any class (not abstract or Interface), without thinking of Object class. Because Object extention will be done automatically for you.

Alpay
  • 1,350
  • 2
  • 24
  • 56
-1

Here, I will suggest, the keyword is EXPLICIT. You can EXPLICITLY extend only one class. But if you don't do that Java will implicitly extend your class with Object class. If you have done that Object must be somewhere up in the hierarchy of the inherited class.

mawia
  • 9,169
  • 14
  • 48
  • 57