Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we inherit from Object?

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?

like image 593
There is nothing we can do Avatar asked Jun 02 '10 18:06

There is nothing we can do


People also ask

Do all classes inherit from object?

All classes inherit from java. lang. Object , although extends Object is (generally) not written out anywhere.

Does string inherit from object?

Yes, 'str' is a string literal , not a string object . A string literal has access to all of a string's objects and methods because javascript will temporarily cast a string literal as a string object in order to run the desired method.

Do all classes inherit from object in Python?

Each class in Python, by default, inherits from the object base class.


2 Answers

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.

like image 102
CheesePls Avatar answered Oct 23 '22 13:10

CheesePls


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).

like image 42
ewernli Avatar answered Oct 23 '22 15:10

ewernli