Inheritance
Introduction
Object class
Every class in Java is descended from the java.lang.Object class. If no inheritance is specified when a class is defined, the superclass of the class is Object.
The Object class
java.lang.Object
How the Object class works
- The Object class in the java.lang package is the superclass for all classes. As a result, its methods are available to all classes.
- The hash code for an object is a hexadecimal number that identifies the object's location in memory.
- When creating classes, it's a common practice to override the toString and equals methods so they work appropriately for each class.
- In general, you don't need to override the finalize method for an object. That's because the garbage collector automatically reclaims the memory of an object whenever it needs to and before it does that, it calls the finalize method of the object.
Methods of the Object class
Method | Description |
---|---|
toString() | Returns a String object containing the class name, an @ symbol, and the object's hash code. |
equals(Object) | Returns true (boolean) if this object points to the same space in memory as the specified object. Otherwise, it returns false, even if both objects contain the same data. |
getClass() | Returns a Class object that represents the type of this object. |
clone() | Returns a copy of this object as an Object object (the Cloneable interface must be implemented). |
hashCode() | Returns the hash code (int) for this object. |
finalize() | Called by the garbage collector when it determines that there are no more references to the object. |