by Dinesh Thakur Category: Inheritance

All the classes in Java that you have defined so far are subclasses by default whether or not you have specified the superclass. The class which all classes in Java are descendent of (directly or indirectly) is java .lang. Object class. So each class inherits the instance methods of Object class. It is important to be familiar with the methods provided by the Object class so that you can use them in your class.

public String toString (): This method returns a string representation of an object. The default implementation returns the name of the class followed by , @' and the hexadecimal representation for the object. You can override this method in your classes, to return your own String object for your class.

public boolean equals (Object obj): This method compares two objects for equality and returns true if they are equal and false otherwise.

public final class getClass (): This method returns a class Object (package Java language that contains information about the object's type, such as its classname, its superclass and the interfaces it implements.

Public int hashcode (): This method returns the hashcode value associated with the invoking object.

protected Object clone() throws CloneNotSupportedException : This method creates a copy of the object on which it is called.

Protected void finalize() throws Throwable: This method is called by the garbage collector on an object that becomes garbage. An object becomes garbage when there are no more references to it.

class Rectangle extends Object

     private double length,breadth;
     Rectangle(double x,double y)
       length = x ;
       breadth = y ;
   public void area()
     System.out.println("Area of Rectangle is = " + breadth);
   public void circumference()
   System.out.println("Circumferen of Rectangle is= "+2*(length+breadth));
 class ObjectClass
     public static void main(String[] args)
           Rectangle r = new Rectangle(10,20);
           Rectangle rl = new Rectangle(10,20);
           System.out.println("String Representation = " + r.toString());
           System.out.println("Class Name = " + r.getClass());
           System.out.println("Hash Code  = " + r.hashCode());
           System.out.println("r.rquals(rl) = " + r.equals(rl));

Object Class in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles