views:

101

answers:

3

Guys do we inherit from Object like from any other class (except of course that we don't have to explicitly state that) or there is some special privileges to Object class and it's not inherited as other classes?

+2  A: 

No it's the same. Here the excerpt from JLS 8.1.3:

If the class declaration for any other class has no extends clause, then the class has the class Object as its implicit direct superclass.

Of course, Object itself is a bit special (JLS):

Each class except Object is an extension of (that is, a subclass of) a single existing class (§8.1.3) and may implement interfaces (§8.1.4).

ewernli
And what happen if the class has extends clause, because you do not specify that.
There is nothing we can do
@Knowing All classes inherit directly or indirectly from `Object`. The inheritance relation can of course not form a cycle. The `Object` class is the only one which inherits from nobody, it's the top of hierarchy. http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html There's no difference between inheriting from `Object` or from another class, and except primitive types, everything is an object. Even arrays are objects http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html.
ewernli
@ewernli so if you saying there is no difference between inheriting from an object and inheriting from any other class why can we inherit from more than one class in example like this: public class MyClass extends YourClass{}?
There is nothing we can do
@Knowing When you call a method, the method is looked up using the inheritance chain. If `A` inherits `B`, which inherits `Object`, the method is looked for in `A`, then `B`, then `Object`. Of course inheriting from `B` or from `Object` is not the same. By "no difference" I mean that inheriting from `B` or `Object` follow nevertheless the same rules; there isn't something special happening when we inherit *directly* from `Object`, it's just regular inheritance. Which is how I understood your original question. All classes inherit directly (like `B`) or indirectly (like `A`) from `Object`.
ewernli
@ewernli so is inheriting from Object done according to the same rules or not because I can't really get this info from your last comment.
There is nothing we can do
@Knowing Yes. The same rules apply when you inherit from `Object` or any other class.
ewernli
@ewernli so how is it possible to have class MyClass extends YourClass{}; In this case we inheriting from two classes (implicitly from Object and explicitly from MyClass) and Java do not allow multiple inheritance? Are you sure the same rules apply when inheriting from Object or there is an exception made (little rules bending) for a situation when Object is concerned?
There is nothing we can do
@Knowing Think of it like this: single inheritance graph is a *tree*. You have only one direct parent. Multiple inheritance is forbidden because it creates a *diamond* and is a problem that happen only if you have more than one direct parents. There is no little bending.
ewernli
@ewernli if you saying that there is bending, how would you explain the fact that it is possible to inherit at the same time from Object and YourClass? Assuming that there is no difference between inheriting from Object and any other class,( as you've stated in one of your previous answers). And your last comment is absolutely off topic. I didin't ask about difference between single and multiple inheritance and that's what you trying to explain in your last comment.
There is nothing we can do
@Knowing I said there is NO bending and I talked about multiple inheritance because you asked for it in in your 4th comment. Inheritance (object) = descendant of (human). If you are a descendant of Xxxx, all your kids will be as well. I don't know what's not clear. Apparently I can't make myself clear, so I suggest we stop here, I won't be able to explain it better anyway.
ewernli
@ewernli I think that you just can't admit that there is a bending in case of Object because you have multiple inheritance everytime you type class My extends Yours{}; And you still didn't answer to my question which I asked in last comment to explain the fact that it is possible to inherit at the same time from Object and YourClass? Are you avoiding an answer to that question or you just don't know? And in my 4th comment I didn't ask about explanation what is multiple inheritance. Cite me if you can to show me where I ask for it.
There is nothing we can do
@ewernli Just came back from a run and yes you are right I was wrong. Of course I accept your answer.
There is nothing we can do
A: 

Everything is an Object in Java. All of the methods of Object (toString(), wait(), etc.) can be called on any instance of any Java class.

FarmBoy
+3  A: 

Every class in Java IS an Object. They behave like Objects, they can be added to collections of type Object, they can use any method defined in Object.

So, YES, everything (except primitives) inherit from Object in Java.

EDIT:Java takes the approach of "Everything is an Object". It sort of forces Object Oriented programming.

Example:

     If class A does not extend another class it inherently extends Object.

     If class A extends another class B, it is extends Object as well since B must have extended Object.
CheesePls
So how is it yes or no? I'm asking because I'm getting two answers which both of them are contradicting each other.
There is nothing we can do
The answer is "we inherit from Object like from any other class (except of course that we don't have to explicitly state that)"
CheesePls