Menu
Question Index
...

public class CopyTest {
    public static void main(String... str) {
        Hello1 myClass1 = new Hello1("Mahi", 575);
        myClass1.setId(555);
        Hello1 clone = myClass1.clone();
        Hello1 copy = clone.copy();
        myClass1.setName("Nani");
        Hello1 classCopy = myClass1.copy();
        myClass1.setId(255);
        CloneTest cloneTest = new CloneTest();
        cloneTest.setVal(122);
        myClass1.setTest(cloneTest);
        System.out.print(copy.equals(clone) + " " + (copy == clone) + " " + (copy == classCopy) + " " + copy.equals(classCopy) + " ");
        System.out.print(clone.getTest().val + " ");
        System.out.println(classCopy.getTest().val);
    }
}

class CloneTest {
    int val = 50;
    @Override
    public CloneTest clone() {
        CloneTest result = new CloneTest();
        result.setVal(val);
        return result;
    }

    public void setVal(int val) {
        this.val = val;
    }
}

class Hello1 {

    String name;
    Integer id;
    CloneTest test;

    public CloneTest getTest() {
        return test;
    }
    
    public void setTest(CloneTest test) {
        this.test = test;
    }
    
    @Override
    public Hello1 clone() {
        Hello1 myClass = new Hello1(name, id);
        myClass.setTest(test.clone());
        return myClass;
    }
    
    public Hello1 copy() {
        Hello1 myClass = new Hello1(name, id);
        myClass.setTest(test);
        return myClass;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Override
    public boolean equals(Object arg0) {
        return ((Hello1) arg0).name.equals(name) && ((Hello1) arg0).id.equals(id);
    }
    
    public Hello1(String name, Integer id) {
        this.name = name;
        this.id = id;
        setTest(new CloneTest());
    }
}


false false false false 50 50
true false false false 50 50
Compilation Error
Runtime Error

Doubts

Problems

Topic: Java Multiple Methods In One Class

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App