0% found this document useful (0 votes)
28 views2 pages

Java Lec-36 Object Class.2f1b096

The object class is the parent class of all classes in Java. It is the topmost class and all other classes directly or indirectly inherit from it. The object class contains common methods like clone(), equals(), finalize(), getClass(), and toString(). The toString() method returns a string representation of an object and can be overridden to print the data of custom objects as demonstrated in the sample program.

Uploaded by

Dnyanesh Chopade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Java Lec-36 Object Class.2f1b096

The object class is the parent class of all classes in Java. It is the topmost class and all other classes directly or indirectly inherit from it. The object class contains common methods like clone(), equals(), finalize(), getClass(), and toString(). The toString() method returns a string representation of an object and can be overridden to print the data of custom objects as demonstrated in the sample program.

Uploaded by

Dnyanesh Chopade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-36
Topic: Object class

 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.

 Every class of Java is either directly or indirectly inherited from object class.

 The object class is beneficial, if you want to refer any object whose type you
don’t know at run-time.

Method of object class :


1. clone()
Creates and return a copy of this object.

2. equals()
This method checks whether two objects are equal or not.

3. finalize()
This method is called by the garbage collector when object is destroyed
form the memory.

4. getClass ()
Returns the run-time class of an object.

5. toString ()
Returns a string representation of an object.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Program to demonstrate How to override toString() method to print object


data.
import java.util.*;

class Employee
{
int emp_no;
String name;
double salary;

Employee(int a, String b, double c)


{
emp_no=a;
name=b;
salary=c;
}
public String toString()
{
String str = "Emp["+emp_no+","+name+","+salary+"]";
return (str);
}
public static void main(String [] args)
{
Employee e1 = new Employee(10,"Atul",50000.0);
Employee e2 = new Employee(20,"Vinay",60000.0);
System.out.println(e1);
System.out.println(e2);
}
}
Output:

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like