HudaTutorials.com

java lang Object Class - java.lang.Object Class in Java

Last updated on

java.lang.Object Class

Object Class in Java

Java Object class is the root of the class hierarchy. Every class has Object as a super class. All objects, including arrays, implement the methods of this class.

Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a Class does not extend any other class then it is direct child class of Object and if extends other class then it is an indirectly derived. Therefore the Object class methods are available to all Java classes. Hence Object class acts as a root of inheritance hierarchy in any Java Program.

The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java. The Object class is beneficial if you want to refer any object whose type you don't know. Notice that parent class reference variable can refer the child class object, known as upcasting.

What is java.lang.Object in Java ?

The java.lang.Object is a class in Java . This is the root of the class hierarchy . Object class is present in java.lang package . Every class in Java is directly or indirectly derived from the java.lang.Object class . If a Class does not extend any other class then it is direct child class of Object . The java.lang.Object class methods are available to all Java classes . Hence Object class acts as a root of inheritance hierarchy in any Java Program .

What is a class object in Java ?

An object is the instance of the class , which helps programmers to use variables and methods from inside the class . A class is used to bind data as well as methods together as a single unit . object acts as a variable of the class . Classes have logical existence. Objects have a physical existence .

Which package has Object class ?

The java.lang package has Object class. Object class is the root of the class hierarchy. Every class has Object as a super class.

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 .

How do you create a class object in Java ?

In Java , the new keyword is used to create new objects .

  1. Declaration − A variable declaration with a variable name with an object type .
  2. Instantiation − The 'new' keyword is used to create the object .
  3. Initialization − The 'new' keyword is followed by a call to a constructor .

What is object type in Java ?

The Object class is the parent class of all the classes in java by default . In other words, it is the topmost class of Java . The Object class is beneficial if you want to refer any object whose type you don't know . Notice that parent class reference variable can refer the child class object , known as up casting .

What are the common behaviors of all the objects in Java ?

The Object class provides some common behaviors to all the objects such as object can be compared , object can be cloned , object can be notified etc .

What is Java object hashCode ?

Returns a hash code value for the object . Whenever it is invoked on the same object more than once during an execution of a Java application , the hashCode method must consistently return the same integer , provided no information used in equals comparisons on the object is modified .

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

What is getClass () in Java ?

The java.lang.Object.getClass() method returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.

How do you compare objects in Java ?

In Java equals() method is used to compare equality of two Objects . The equality can be compared in two ways: Shallow comparison: The default implementation of equals method is defined in java.lang.Object class which simply checks if two Object references (say x and y) refer to the same Object .

Why object class is superclass of all classes in Java ?

All classes in Java by default extend the Object class , that's why Object is superclass of every class in Java . Class Object is the root of the class hierarchy . All objects , including arrays , implement the methods of this class .

How do you clone an object in Java ?

The clone() method of Object class is used to clone an object . The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create . If we don't implement Cloneable interface , clone() method generates CloneNotSupportedException . The clone() method is defined in the Object class .

What is the Difference Between Class and Object ?

Class and Object are the essential part of Object Oriented Programming ( OOP ) . A class can be considered as a construct which encapsulates a group of variables and methods . An object acts as a member or instance of that class .

What are the Differences Between Class and Object in Java ?

Following are the differences between Class and Object in Java .

Class Object
A class is a blueprint from which you can create the instance, i.e. objects . An object is the instance of the class , which helps programmers to use variables and methods from inside the class .
A class is used to bind data as well as methods together as a single unit . object acts as a variable of the class .
Classes have logical existence . Objects have a physical existence .
A class doesn't take any memory spaces when a programmer creates one . An object takes memory when a programmer creates one .
The class has to be declared only once . Objects can be declared several times depending on the requirement .

What are the object methods in Java ?

Java Object is the superclass for all the classes in Java. All the methods of Object class can be used by all the subclasses and arrays. The Object class provides different methods to perform different operations on objects.

Java Object getClass()

returns the class name of the object

Java Object hashCode()

returns the hashcode value of the object

Java Object toString()

converts an object into the string

Java Object equals()

checks if two objects are equal

Java Object clone()

creates a copy of the object

What methods would you overwrite in Java Lang object class ?

  • clone()
  • equals()
  • finalize()
  • hashCode()
  • toString()

Following Java Object class example you can learn how to use Java Object class.

java.lang.Object Class Example

/*  Java Object class example
    Save with file name ObjectExample1.java */
public class ObjectExample1
{
	public static void main(String args[])
	{
		// Java Object CLASS DECLARATION
		Object obj;
		// CREATE INSTANCE FOR Java Object CLASS
		obj = new Object();
		// Java Object CLASS OUTPUT
		// NOTHING TO WORRRY THERE IS NO VALUE
		// IT PRINTS SOME CODE
		System.out.println("Object value : " + obj);
	}
}

java.lang.Object Class Example 2

/*  Java Object Class Example with String Class
    Save with file name ObjectExample2.java */
public class ObjectExample2
{
	public static void main(String args[])
	{
		// String CLASS DECLARATION
		String s;
		// CREATE INSTANCE FOR String CLASS
		s = new String("Huda Tutorials");
		// Java Object CLASS DECLARATION
		Object obj;
		// TYPECAST String CLASS INSTANCE TO
		// Java Object CLASS INSTANCE
		obj = (Object) s;
		// Java Object CLASS OUTPUT
		System.out.println("Object value : " + obj);
		// IT RETURNS RUNTIME CLASS OF THIS OBJECT
		// java.lang.String
		System.out.println("Object Class : " + obj.getClass());
	}
}