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 it.
New contributor
Prakhar Garg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.