Tuesday 22 July 2014

Cloning in Java

Cloning means makes an identical copy of something. Object class provides clone() method to perform cloning.

If you want to clone an object 'obj' of class 'A', then class 'A'must implement the Cloneable interface. If a class implements Cloneable interface, then it is making sure that calling clone method on objects of this class is valid. If you tries to use clone method without implementing Cloneable interface, then 'java.lang.CloneNotSupportedException' thrown.

class Laptop implements Cloneable{
    String processor = "4th Gen Intel Core i3";
    double screenSize = 43.9;
    int ramSize = 6;
    int hardDiskSize = 500;
    String brand = "XPS";
    String videoCard = "Integrated Graphics";     
    
    public void setProcesstor(String processor){
        this.processor = processor;
    }
    
    public void setScreenSize(double screenSize){
        this.screenSize = screenSize;
    }
    
    
    public void setRAM(int ramSize){
        this.ramSize = ramSize;
    }
    
    
    public void setHardDist(int hardDiskSize){
        this.hardDiskSize = hardDiskSize;
    }
    
    
    public void setBrand(String brand){
        this.brand = brand;
    }
    
    
    public void setVideoCard(String videoCard){
        this.videoCard = videoCard;
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
    
    @Override
    public String toString(){
        StringBuilder str = new StringBuilder();
        str.append("----------------------------");
        str.append("\nYour laptop configuration");
        str.append("\n----------------------------");
        str.append("\nProcessor :" + processor );
        str.append("\nscreenSize :" + screenSize +"cm" );
        str.append("\nramSize :" + ramSize +"GB");
        str.append("\nhardDiskSize :" + hardDiskSize +"GB" );
        str.append("\nbrand :" + brand );
        str.append("\nvideoCard :" + videoCard );       
        return str.toString();
    }
    
    @Override
    public boolean equals(Object obj){
        if(obj != null){
            if(obj instanceof Laptop){
                Laptop lap = (Laptop) obj;
                
                return (this.brand.equals(lap.brand) &&
                        this.hardDiskSize == lap.hardDiskSize &&
                        this.processor.equals(lap.processor)&&
                        this.ramSize == lap.ramSize &&
                        this.screenSize == lap.screenSize &&
                        this.videoCard.equals(lap.videoCard));
            }            
        }
        return false;
    }
}

public class CloningEx {
    public static void main(String args[]) throws CloneNotSupportedException{
        Laptop lap1 = new Laptop();
        
        lap1.setProcesstor("4th Gen Intel Core i7");
        lap1.setBrand("Alienware");
        lap1.setHardDist(1000);
        lap1.setRAM(8);
        lap1.setScreenSize(43.9);
        lap1.setVideoCard("Integrated Graphics");
        
        Laptop lap2 = (Laptop)lap1.clone();
        
        System.out.println(lap1);
        System.out.println(lap2);
        
        System.out.println("lap1.equals(lap2) :" + lap1.equals(lap2));
        System.out.println("lap1==lap2 :" + (lap1==lap2));
        System.out.print("Is lap1 and lap2 belongs to sameclass :");
        System.out.println((lap1.getClass().equals(lap2.getClass())));
    }
}


Output
----------------------------
Your laptop configuration
----------------------------
Processor :4th Gen Intel Core i7
screenSize :43.9cm
ramSize :8GB
hardDiskSize :1000GB
brand :Alienware
videoCard :Integrated Graphics
----------------------------
Your laptop configuration
----------------------------
Processor :4th Gen Intel Core i7
screenSize :43.9cm
ramSize :8GB
hardDiskSize :1000GB
brand :Alienware
videoCard :Integrated Graphics
lap1.equals(lap2) :true
lap1==lap2 :false
Is lap1 and lap2 belongs to sameclass :true





                                                             Home

No comments:

Post a Comment