为什么 Java 中的每个对象都隐式扩展 java.lang.Object 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19114997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:13:49  来源:igfitidea点击:

Why does every object in Java implicitly extend java.lang.Object class?

javaobjectinheritancedesign-patterns

提问by codeMan

I have been programming in Java for quite some time, but when I tried to explain what an java.lang.Objectclass is to a friend, I could not come up with more than a simple one-liner:

我已经用 Java 编程有一段时间了,但是当我试图向java.lang.Object朋友解释类是什么时,我只能想出一个简单的单行代码:

All objects in Java extend java.lang.Objectimplicitly

Java 中的所有对象都java.lang.Object隐式扩展

I was not quite sure why it should do so.

我不太确定为什么要这样做。

So, I looked upon the source code on GrepCode, hoping that I can find some clues. Now I know what a java.lang.Objectis and what it does, I want to know if there was any specific reason as to why it was designed this way.

于是,我查看了GrepCode上的源代码,希望能找到一些线索。现在我知道 ajava.lang.Object是什么以及它的作用,我想知道是否有任何具体的原因来解释为什么它是这样设计的。

My question still prevails: why should every object extend java.lang.Object?

我的问题仍然存在:为什么每个对象都应该扩展java.lang.Object

回答by Suresh Atta

I would say Design. Common/Mandatory methods which every Objectshould support written there and extending that class as a language specification.

我会说Design。每个人都Object应该支持的通用/强制方法在那里编写并将该类扩展为语言规范。

You find the reasons here in Official Docs.

您可以在官方文档中找到原因。

If we are saying this is an Object,They must have the common methods, Which defined/decided by API.

如果我们说这是一个Object,它们必须有共同的方法,由 定义/决定API

Imagine the below methods for every class on your Own.

想象一下您自己的每个类的以下方法。

protected Object clone() throws CloneNotSupportedException
      Creates and returns a copy of this object.


public boolean equals(Object obj)
      Indicates whether some other object is "equal to" this one.


protected void finalize() throws Throwable
      Called by the garbage collector on an object when garbage
      collection determines that there are no more references to the object


public final Class getClass()
      Returns the runtime class of an object.


public int hashCode()
      Returns a hash code value for the object.


public String toString()
      Returns a string representation of the object.

The notify, notifyAll, and wait methods of Object all play a part in synchronizing the activities of independently running threads in a program:

Object 的 notify、notifyAll 和 wait 方法都在同步程序中独立运行的线程的活动中发挥作用:

public final void notify()
public final void notifyAll()
public final void wait()
public final void wait(long timeout)
public final void wait(long timeout, int nanos) 

So to reduce the pain, created a common and standard API.

所以为了减轻痛苦,创造了一个共同的标准API

回答by Maroun

See the docs:

查看文档

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class.

Object 类位于 java.lang 包中,位于类层次结构树的顶部。每个类都是 Object 类的直接或间接后代。您使用或编写的每个类都继承了 Object 的实例方法。您不需要使用任何这些方法,但是,如果您选择这样做,您可能需要使用特定于您的类的代码来覆盖它们。

The Objectclass simply defines the basic state that allobjects must have - Like comparing it to other objects.

Object类简单地定义了基本状态的所有对象必须有-像比较它与其他对象。

It's the parentclass of everything. It simply provides kind of templateto all the derived objects.

它是一切的类。它只是为所有派生对象提供一种模板

回答by Konstantin Yovkov

This is how the language is designed. Every object will inherit from the base class Object. This means that it's guaranteed for every object there will be certain methods, like toString(), equals(), hashCode(), etc.

这就是语言的设计方式。每个对象都将从基类继承Object。这意味着,它保证每一个对象会有一定的方法,如toString()equals()hashCode(),等。

回答by Aneesh

It's a java design decision. It puts to use the concept of inheritance and re-usabilty. This ensures that all classes have some basic methods like wait(), toString()etc.

这是一个java设计决定。它使用了继承和重用的概念。这将确保所有类都有一些基本的方法,如wait()toString()等。

回答by René Link

I would say that the reason is to have a common API for all objects in java to supports basic functionality like

我会说原因是为 Java 中的所有对象提供一个通用 API 以支持基本功能,例如

  • synchronization- wait, notify, notifyAll
  • garbage collection- finalize
  • collection support- hashCode, equals
  • object cloning- clone
  • 同步- wait, notify, notifyAll
  • 垃圾收集——finalize
  • 收藏支持- hashCode,equals
  • 对象克隆-clone

And every object

和每一个对象

  • has a class it belongs to - getClass
  • can represent itself as a string, because we are humans and can read strings - toString
  • 有一个它所属的类 - getClass
  • 可以将自己表示为字符串,因为我们是人类并且可以读取字符串 - toString

回答by Yasa

Object class is the most super class of java programming, It has predefined methods according to types, you can use those methods. & you don't need to extends object class anymore & anywhere it's implicitly there

Object类是java编程中最超类的类,它根据类型预定义了方法,你可以使用这些方法。& 你不再需要扩展对象类 & 它隐式存在的任何地方

回答by Nawed Shaikh

This is done so as most of the basic functions like toString() etc would be automatically inherited and to your next question this is NOTmultiple inheritence it is multilevelinheritence... In multiple inheritencesingle class is derived from 2 or more base class whereas in multilevel as you have said it has a base class which is itself derived from Object class

这样做是因为像 toString() 等大多数基本函数将被自动继承,对于您的下一个问题,这不是多重继承,而是多级继承......在多重继承中,单个类派生自 2 个或更多基类,而在多级中,正如您所说,它有一个基类,它本身是从 Object 类派生的

回答by billc.cn

I think the most important use of Objectis not to provide common methods like toString()but to provide a common typethat would hold all reference types.

我认为最重要的用途Object是不提供像常用的方法toString(),但提供了一个通用,将持有的所有引用类型。

C++ don't have an Objectequivalent and people are still happy. But since Java don't have pointers and C++-like templates, Objectis required to make implementations of Collections, etc. possible.

C++ 没有Object等价物,人们仍然很高兴。但是由于 Java 没有指针和类似 C++ 的模板,Object因此需要使Collections 等的实现成为可能。

See also on discussions on reference and primitive types.

另请参阅有关引用和原始类型的讨论。

回答by user1425349

Every Class extends Object class implicitly so that they provide basic features which according to Java recommendation every class should have. Such as clone(), equals(), hashCode(), toString(), etc.

每个类都隐式地扩展了 Object 类,以便它们提供根据 Java 建议每个类应该具有的基本功能。如clone()、equals()、hashCode()、toString()等。

By implicitly, it means that if you are not extending any class then only compiler will implicitly extends Object class.But if class already extends other class then compiler will not extend Object class. For eg. Class A{ }

隐式,这意味着如果你没有扩展任何类,那么只有编译器会隐式扩展 Object 类。但是如果类已经扩展了其他类,那么编译器将不会扩展 Object 类。例如。A类{}

Class B extends A{ }

B 类扩展 A{ }

Here compiler will implicitly add extends Object class in class A declaration.

这里编译器会在类 A 声明中隐式添加 extends Object 类。

Class A extends Object{ }

A类扩展对象{}

Class B extends A{ }

B 类扩展 A{ }

As class A extends Object class so it will provide basic functionality of Object class such as equals(), toString(),etc. And since Class B extends class A which implicitly extends Class Object, so class B also provides all those features.

由于类 A 扩展了 Object 类,因此它将提供 Object 类的基本功能,例如 equals()、toString() 等。由于 B 类扩展了 A 类,而 A 类隐式扩展了类对象,因此 B 类也提供了所有这些功能。

Thus by following this approach every class objects(variables) complies to features which every Java Object should have, without going for Multiple Inheritance (a class extending more than one class) which Java doesn't allows. This approach follows Multi-Level Inheritance.

因此,通过遵循这种方法,每个类对象(变量)都符合每个 Java 对象应该具有的特性,而不需要 Java 不允许的多重继承(扩展多个类的类)。这种方法遵循多级继承。

回答by Aaditya

Quoting Head first Java 2nd edition:

引用 Head first Java 2nd edition:

Without a common superclass for everything in Java, there'd be no way for the developers of Java to create classes with methods that could take your custom types... types they never knew about when they wrote the ArrayList class.

如果没有 Java 中所有内容的通用超类,Java 开发人员就无法创建带有可以接受自定义类型的方法的类……他们在编写 ArrayList 类时从未知道的类型。

Which essentially explains the need of a generic predefined class type in Java, which can be used to implement the different features provided by the language.

这基本上解释了在 Java 中需要一个通用的预定义类类型,它可以用来实现语言提供的不同功能。