Thursday, August 8, 2024

Object class

Object class:

// Although you don't see this explicitly in most classes,

// every class in Java extends Object by default.

class Object

{

    // Some methods available in Object class:

    // - toString()

    // - equals(Object obj)

    // - hashCode()

    // - getClass()

    // - clone()

}             

Object is a predefined class in Java. It is the root of the class hierarchy. Every class in Java implicitly extends the Object class if no other superclass is specified

FAQ: What is the superclass of all classes in Java?

The Object class is the superclass (also known as the parent or base class) for all classes in Java.

class A extends Object

{

    // Since A doesn't extend any other class,

    // it automatically extends Object.

}

 

class String extends Object {

    // String class, like any other class,

    // ultimately extends Object.

}                            

FAQ:  What is object class and when should we use object class?

·  indirectly inherits from the Object class. It is the superclass for all classes in Java.

·  Since Object is the ultimate parent class, it provides several fundamental methods that are inherited by all Java classes, such as:

  • toString(): Returns a string representation of the object.
  • equals(Object obj): Compares the current object with another object for equality.
  • hashCode(): Returns a hash code value for the object.
  • getClass(): Returns the runtime class of the object.
  • clone(): Creates and returns a copy of the object (if the class implements the Cloneable interface).
  • finalize(): Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Ex:

// An Object reference variable can store references to any type of object,

// including instances of Integer, Float, String, etc.

// Example: Storing an int value in an Object reference variable

 

Object obj;

 

// Boxing: Storing an int value in an Object reference variable

obj = 10; // int value is automatically boxed into an Integer object

 

// Boxing: Storing a float value in an Object reference variable

obj = 10.5f; // float value is automatically boxed into a Float object

 

// Storing a String value in an Object reference variable

obj = "Hello, World!";

 

an Object reference variable can store different types of values, including primitive values through autoboxing:

Explanation

In Java, the Object class is the root of the class hierarchy. This means that an Object reference variable can refer to an object of any class, including instances of wrapper classes like Integer, Float, and String.

 

When you assign a primitive value to an Object reference, Java automatically converts the primitive value into its corresponding wrapper class object through a process called autoboxing.

            +-------------------+       +-------------+

            |   Object ref var   |----->|  Integer(10) | (Boxing: int to Integer)

            |      obj           |       +-------------+

            +-------------------+

                      |

                      |                 +-------------+

                      |---------------> |  Float(10.5) | (Boxing: float to Float)

                      |                 +-------------+

                      |

                      |                 +-------------+

                      |---------------> | "Hello"      | (String is already an object)

                                        +-------------+

 

·  Storing an int value:

  • When you assign an int value like 10 to obj, Java performs autoboxing, converting the int to an Integer object. Now, obj holds a reference to this Integer object.

·  Storing a float value:

  • Similarly, when you assign a float value like 10.5f to obj, it is autoboxed into a Float object. Now, obj holds a reference to this Float object.

·  Storing a String value:

  • When you assign a String value like "Hello" to obj, it directly holds a reference to the String object, as String is already an object type.

 

 

package InterfaceBasics;

public class TestObjectClassReferenceVariable {

               public static void main(String[] args)

               {

                              int i= 10;

                              float f=10.85f;   

                              // object ref variable , we can store int value, float value, String value

                              // store int value in obj ref variable

                              Object oref = i;

                              //            10

                              //   oref=  10

                              System.out.println("object can store int val="+oref);

                              // store float value in obj ref var

                              oref = f;

                              //     10.85

                              //  oref =  10.85

                              System.out.println("object can store float val="+ oref);

                              // store String value in obj ref var

                              oref = "Ram";

                             

                              System.out.println("object can store String val="+oref);

                              // By using Object variable,  we can store any type of value

                             

                              //when we want to store any data type val, we will go for Object class.

                              // HW store char value in obj ref variable and display it

                              // HW store boolean value in obj ref variable and display it

               }

}

                             

Object array:

package InterfaceBasics;

public class TestObjectClass {

               public static void main(String[] args)

               {

                              int a [] = {10,20 ,30};

                              float farr[] = {2.4f,3.5f,4.5f};

                              char charr [] = {'A','B','C'};

                             

                              // Using object arr -   1 int  + 1 float +  1 char ....etc

                              // define obj arr - store  {10, 2.4f,'A', "Ramu"};

                              Object oarr [] = {10 ,2.4f,'A',"Ramu"};

//(or )

               // create object array using new  k/w with size =5

                              Object oarr2 [] =  new Object [5];//  0 to size-1 i.e o  to 5-1=4

                             

                              // store  10 value at index no=0

                              oarr2[0] = 10;

                              // store  2.3f value at index no=1

                              oarr2[1] = 2.3f;

                              // store  'A' value at index no=2

                              oarr2[2] = 'A';

                              // store  "Ram" value at index no=3

                              oarr2[3] = "Ram";

                              // store   true value at index no=4

                              oarr2[4] =  true;

                             

                              // get all values from Object array using 2 ways

                              // HW 1 . display all values using for loop with index no

                                                           

                              // HW 2 display Object array values using 'for each' loop  ?                                         

               }

}

 

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...