Java.lang.Object class:

  • Object is the root or super class of the class hierarchy. Which is present in java.lang package.
  • All predefined classes and user defined classes are sub classed from Object class

Why object class is super class for all java classes:

  1. Re-usability:
    • Every object has 11 common properties.
    • These properties must be implemented by every class developer.
    • So to reduce burden on developer SUN developed a class called Object by implementing all these 11 properties with 11 methods.
    • All these methods have generic logic common for all sub classes. if this logic is not satisfying subclass requirement then subclass can override it.
     
  2. Run time polymorphism:
    • To achieve run time polymorphism so that we can write single method to receive and send any type of class object as argument and as return type.

Why this class name is chosen as object?

  • As per coding standards , class name must convey the operations doing by that class.
  • So this class name is chosen as Object as it has all operations related to object.
  • These operations are chosen based on real world object's behavior.

Common functionalities of every class object: 

  1. Comparing two objects: 

    • public boolean equals(Object obj)
  2. Retrieving hashcode: 

    • public int hashcode()
  3. Retrieving the run time class object reference:

    • public final Class getClass()
  4. Retrieving object information in String format:

    • public String toString()
  5. Cloning object: 

    • protected Object clone()
                      throws CloneNotSupportedException
  6. Object clean-up code/ resources releasing code: 

    •  protected void finalize()
                       throws Throwable
  7. To wait current thread until another thread invokes the notify():

    • public final void wait()
                      throws InterruptedException
  8. To wait current thread until another thread invokes the notify() specified amount of time:

    • public final void wait(long timeout)
                      throws InterruptedException
  9.  To wait current thread until another thread invokes the notify() specified amount of time

    • public final void wait(long timeout,int nanos)
                      throws InterruptedException
  10. Notify about object lock availability to waiting thread: 

    • public final void notify()
  11. Notify about object lock availability to waiting threads: 

    • public final void notifyAll() 
  • All above operations are implemented with generic logic that is common for all objects, so that developer can avoid implementing these common operations in every class.
  • Also Oracle ensures that all these operations are overridden by developers with the same method prototype. This feature will provide run time polymorphism.

What are the methods we can override in sub class from Object class:

  1. equals()
  2. hashCode()
  3. toString()
  4. clone()
  5. finalize()

Example program on toString() method :

  1. package com.instanceofjavaforus;
  2.  
  3. public class ToStringDemo {
  4.     int id;
  5.    String name;
  6.  
  7. public String toString(){
  8.  return id+" "+name;
  9.  }
  10.  
  11.  public int getId() {
  12.    return id;
  13.  }
  14.  
  15. public void setId(int id) {
  16.   this.id = id;
  17.  }
  18.  
  19.  public String getName() {
  20.       return name;
  21.  }
  22.  
  23.  public void setName(String name) {
  24.     this.name = name;
  25.   }
  26.  
  27.   public static void main(String[] args) {
  28.  
  29.    ToStringDemo obj = new ToStringDemo();
  30.      obj.setId(1);
  31.      obj.setName("abc");
  32.     System.out.println(obj);
  33.  
  34.  }
  35. }

  36. OutPut:
  37. 1 abc


Example program on clone() method:

  1.  
  2. package com.instanceofjava;
  3.  
  4. public class CloneDemo  implements Cloneable  {
  5.   int a=0;
  6.    String name="";
  7.   CloneDemo (int a,String name){
  8.  this.a=a;
  9.  this.name=name;
  10.  }
  11.   public CloneDemo clone() throws CloneNotSupportedException{
  12.  return (CloneDemo ) super.clone();
  13. }
  14.  public static void main(String[] args) {
  15.  CloneDemo e=new CloneDemo (2,"abc");
  16.  System.out.println(e.name);
  17.     try {
  18.  CloneDemo b=e.clone();
  19.  System.out.println(b.name);
  20. } catch (CloneNotSupportedException e1) {
  21.  // TODO Auto-generated catch block
  22.   e1.printStackTrace();
  23. }
  24. }
  25.  
  26. }
  27.  
  28. OutPut:
  29. abc
  30. abc

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu