Object Classes

Object is the super class for the all classes in java. By default every class in java are object type. Whether it is predefine or user define and arrays. In object class only method are there.

How many method are there in object class?

Total 11 methods are there in object class.

Why we use toString()?

We use toString() to convert the any object into String representation or String object. By default JVM call the toString() method.

Syntax:

	
    public String toString() //it return String object only
    

Example of toString() object class:

	
    class A
	{
		private String name;
		private int id;

		public A(String name, int id)
		{
			this.name = name;
			this.id = id;
		}

		public String toString()
		{
			return "Name = "+name+ "\nId = "+id;
		}
	}

	class Test4
	{
		public static void main(String[] arg)
		{
			A a = new A("Manzar", 123);
			System.out.println(a);
		}
	}
    

Output

	
    Name = Manzar
    Id = 123
    

clone() method

Why we use clone() method?

Creating the new object with exactly same object. A new object created with same data it's called object clonning.

The clone() method is defined in the Object class. Syntax of the clone() method is as follows:

Example of clone() method:


	class Employee implements Cloneable
	{
		int id;
		String name;
		public Employee(int id, String name)
		{
			this.id = id;
			this.name = name;
		}
		public Object clone()throws CloneNotSupportedException
			{
				return super.clone();
			}
		}
	public class Test6
	{
	public static void main(String args[])throws CloneNotSupportedException
		{
			Employee emp = new Employee(123,"Akhtar");
			Employee emp2 = (Employee)emp.clone();
			System.out.println(emp.id+" "+emp.name);
			System.out.println(emp2.id+" "+emp2.name);
		}
	}

Output


	123 Akhtar
	123 Akhtar

getClass() method

getClass() is use to get the name of the Class which loaded in the JVM memory.

Example of getClass() method:


	class A
	{	
		public void show()
		{

		}
	}

	class Test
	{
		public static void main(String args[])
		{
			Hello h = new Hello();
			Class c = h.getClass();
			System.out.println(c);
		}
	}

Output


	class A

getName() is use to get the name of the Class without class keyword which loaded in the JVM memory.

Example of getName() method:


	class A
	{	
		public void show()
		{

		}
	}

	class Test
	{
		public static void main(String args[])
		{
			Hello h = new Hello();
            
            //This method will give output with class keyword
			Class c = h.getClass();	
            
			//This method will give output without class keyword
            String name = c.getName();
            	
			System.out.println(c+"\n"+name);
		}
	}

Output


	class A		//Class name with class keyword
	A			//Class name without class keyword
    

finalize() method

We all know that an object is created in memory using new operator. Constructor is used to initialize the properties of that object. When an object is no more required, it must be removed from the memory so that that memory can be reused for other objects.

finalize method is a special method that performs finalization, generally some form of cleanup. Finalize executed when object going to remove from JVM memory.

Life cycle of an object

Methods are representing there life cycle of an object.

System.gc() is method in System class. By default it is a static method.

Example of finalize method:


	class Car
	{
		int regno;
		public Car(int regno)
		{
			this.regno = regno;
			System.out.println("Car Object is created..");
		}
		public void run()
		{
			System.out.println("Car Running..");
		}
		public void finalize()
		{
			System.out.println("Finalize called...");
		}
	public static void main(String args[])
	{
		Car car = new Car(123456);
		car.run();
		car=null;
		System.gc();
		}
	}
	

equals() and hashCode()

equals() method

The equals() method is always used to comapre two strings. It compare the string based on hash code generated by hashCode() method.

equals() method always depends on hashCode() method. equals() method also check class type or data also.

equals() method return type is boolean. It always give result in the form of true or false only.

hashCode() method

hashCode() method generate unique number based on the data. The hashCode() method is used to get the hash code of a string.

hashCode() is one Algorithm which is given by Sun MicroSystems.

If two string is different then there hashCode will be different.

hashCode() method return type is int.

Example of equals() and hashCode() method:


	class Raza
	{
		public static void main(String args[])
		{
			String str = "Hello";
			String str1 = "Hello";
            
            //caomparing the two strings
			boolean b = str.equals(str1);
            
            //getting the hash code of two string
			int i = str.hashCode();
			int f = str1.hashCode();
            
			System.out.println(b);
            
			System.out.println(i);
            
			System.out.println(f);
		}
	}

Output


	true
    69609650
    69609650
        
Advertisement here

Share this Page.