OBJECT CLASS

Object Class


  • Object class is super class (or) base class for all the java classes. Every java class Extends Object class means every java class is subclass to the Object class , without Object class we can’t create any java class.

  • Object class is available under java.lang package.

  • Sample Program:

    A.java

     
                    
                 package com.sdj;
                 class A
                 {
                    public static void main(String[ ] args) 
                    {
                       System.out.println("Hello World!");
                    }
                 }                

      The above program has been changed into the following manner after compilation.

    A.java

     
                    
                 package com.sdj;
                 class A  extends Object
                 {
                    A( )
                    {
                       -----------
                       ------------
                    }
                    public static void main(String[] args) 
                    {
                       System.out.println("Hello World!");
                    }
                 }                

    Example 1:

    B.java

     
                    
                 package com.sdj;
                 class A
                 {
                    String s="javapadho";
                 }
                 class B 
                 {
                    public static void main(String[] args) 
                    {
                       System.out.println("Hello World!");
                       A a1=new A();
                       System.out.println(a1);
                         /*here  a1.toString( ) will be called during the compilation instead of a1*/
                    }
                 }                
    Note
    • According to object class toString( ) method will give fully qualified class with hexadecimal format. like com.sdj.A@123res567.

    Inside object class toString( ) is as follows:

    public String toString()
    {
      return getClass().getName() + + Integer.toHexString(hashCode());
    }

    B.java

     
                    
                 package com.sdj;
                 class A extends Object
                 {
                    A( )
                    {
                       -----------------
                       -----------------
                    }
                    String s="SDJ INFOSOFT PVT LTD";
                 }
                 class B  extends Object
                 {
                    B( )
                    {
                       -----------------
                       -----------------
                    }
                    public static void main(String[] args) 
                    {
                       System.out.println("Hello World!");
                       A a1=new A();
                       System.out.println(a1);
                    }
                 }                

    Overriding toString( ):

    • By default toString( ) will return the fully qualified class name along with the memory address in hexadecimal format.

    • Whenever we call the derived type object (or) pass the derived object as input parameter to System.Out.println( derived object ) then it will call automatically toString( ) during compilation .

    Example 2:

    Before Overriding toString():

     
                    
                 package com.sdj;
                 class Person
                 {
                    String name;
                    int age;
                    public Person(String name,int age) 
                    {
                       this.age=age;
                       this.name=name;
                    }   
                 }
                 public class OverrideToStringDemo 
                 {
                    public static void main(String[] args) 
                    {
                       Person p=new Person("Praveen",24);
                       System.out.println(p);
                    }
                 }                

       Output:

    com.sdj.Person@82ba41

    After Overriding toString():

     
                    
                 package com.sdj;
                 class Person
                 {
                    String name;
                    int age;
                    public Person(String name,int age)
                    {
                       this.age=age;
                       this.name=name;
                    }   
                    public String toString() 
                    {
                       return"The Name of the Person:"+this.name+","+"The age of the person is:"+this.age;
                    }
                 }
                 public class OverrideToStringDemo 
                 {
                    public static void main(String[] args)
                    {
                       Person p=new Person("Praveen",24);
                       System.out.println(p);
                    }
                 }                

    Output:

       The Name of the Person:Praveen, The age of the person is:24
       By overriding toString( ) method we can get desired value rather than the memory address in hexadecimal format as described in the program.

    Example for the comparing two Objects:

     
                    
                 package com.sdj;
                 class Person
                 {
                    String name;
                    int age;
                    public Person(String name,int age)
                    {
                       this.age=age;
                       this.name=name;
                    }   
                    public String toString() 
                    {
                       return"The Name of the Person:"+this.name+","+"The age of the person is:"+this.age;
                    }
                 }
                 public class OverrideToStringDemo 
                 {
                    public static void main(String[] args) 
                    {
                       Person p1=new Person("Praveen",24);
                       System.out.println(p1);
                       Person p2=new Person("Praveen",24);
                       System.out.println(p1==p2);
                       Person p3=new Person("Sujata",23);
                       Person p4=new Person("Sujata",23);
                       System.out.println(p3==p4);
                    }
                 }                

       Output:

    The Name of the Person:Praveen,The age of the person is:24 false false

    Explanation:

    • In the above program p1 ,p2 objects and p3 , p4 objects content type is same but the output of p1==p2 and p3==p4 is false.

    • Because Equals operator will compare two Objects based on the memory location but not based on the content of the object.

    • Two objects have two different memory addresses.

    javapadho
    Memory manage for the comparing two Objects