Relative Content

Tag Archive for javacloning

Why explicit typecasting is required, when we clone an object using Object class clone() method

public class Test implements Cloneable { public static void main(String[] args) throws CloneNotSupportedException { Test t1 = new Test(); System.out.println(t1.clone().getClass().getName()); } } Output: PS C:UsersVICTUSDocumentsjava_work> javac Test.java PS C:UsersVICTUSDocumentsjava_work> java Test Test Since output printed is “Test”, it means the returned dynamic type of new object(cloned) is Test, then why do I need to typecast […]