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

前端 未结 10 2461
青春惊慌失措
青春惊慌失措 2020-11-29 04:21

I have been programming in Java for quite some time, but when I tried to explain what an java.lang.Object class is to a friend, I could not come up with more th

相关标签:
10条回答
  • 2020-11-29 04:51

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

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

    See also on discussions on reference and primitive types.

    0 讨论(0)
  • 2020-11-29 04:52

    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.

    The Object class simply defines the basic state that all objects must have - Like comparing it to other objects.

    It's the parent class of everything. It simply provides kind of template to all the derived objects.

    0 讨论(0)
  • 2020-11-29 04:54

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

    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.

    0 讨论(0)
  • 2020-11-29 04:59

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

    • synchronization - wait, notify, notifyAll
    • garbage collection - finalize
    • collection support - hashCode, equals
    • object cloning - 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
    0 讨论(0)
  • 2020-11-29 05:01

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

    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.

    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:

    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.

    0 讨论(0)
  • 2020-11-29 05:09

    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.

    0 讨论(0)
提交回复
热议问题