0
super.clone() in deep copy in java
//this is an incomplete code public Object clone() // override clone method { Employee emp; try { emp = (Employee) super.clone(); emp.address = (Address) address.clone(); } catch(CloneNotSupportedException e) { return null; // will never happen } return emp; } // Question: why the super.clone() can be downcasted as it is the parent object
1 Réponse
0
Downcasting is allowed when there is a possibility that it succeeds at run time. As long as the super object could reference a subclass object, it works.
You did not share any information about the super class, but you should always check it with the instanceof operator to avoid any runtime errors.