Object Cloning Java





Object Cloning:

  • The object cloning is a way to create exact copy of an object.

  • For this, the java.lang.Cloneable interface must be implemented by the class, and override the method clone(), whose object clone you want to create.

  • The clone() method is defined in the Object class.

    Syntax

    protected Object clone() throws CloneNotSupportedException

Example :

class Employee implements Cloneable
{  
 int id;  
 String name;  
  
 Employee(int id,String name){  
 this.id = id;  
 this.name = name;  
}  
  
 public Object clone()throws CloneNotSupportedException
 {
  return super.clone();  
 }  
  
 public static void main(String args[]){  
 try
 {  
  Employee s1=new Employee(123,"ayan");  
  
  Employee s2=(Employee)s1.clone();  
  
  System.out.println(s1.id+" "+s1.name);  
  System.out.println(s2.id+" "+s2.name);  
  
 }
 catch(CloneNotSupportedException e){}  
  
 }  
}          
OUTPUT :
123 ayan
123 ayan

Next topic is array




 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved