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