Topics Index

Student cloning

If you need explanation Read this topic

If you need Answer Take test on this topic

Student cloning
What will be the output of the following program?
public class CloneTest {
    public static void main(String[] args) {
        Engineer student = new Engineer("Ramu", 446);
        Engineer copy = student.clone();
        System.out.println( + " " + copy.rollNum);
class Engineer {
    public Engineer(String name, int i) { = name;
        rollNum = i;
    String name;
    int rollNum;
    protected Engineer clone() {
        try {
            return (Engineer) super.clone();
        } catch (Exception e) {
            return this;

A. Ramu 446
C. Compilation Error
D. Runtime Error
Topic: clone() Method And cloneable Interface In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is we have the concept of the cloning concept which means creating the duplicate will useful while transfer  ing the objects through network...than receiver side copy of the object will created and operations will performed on the copied object instead of the original object...

here we have the Eng class this class has name and rollNum as properties..from the main() we create the instance for the class Eng by passing the Ramu,446..
next we call the clone() using the student clone method we return the super.clone() which will return the duplicate object for the called Object class here we type converted it into Eng..and we received that object into copy...and we printed the details soo it will print Ramu n 446

Posted by Uday Kumar    2014-12-12 09:08:16

ans a
clone is a tricky method from java.lang.Object class, which is used to create copy of an Object in Java. Intention of clone() method is simple, to provide a cloning mechanism,

java.lang.Object provides default implementation of clone() method in Java. It's declared as protected and native in Object class, so implemented in native code. Since it's convention to return clone() of object by calling super.clone() method, any cloning process eventually reaches to java.lang.Object clone() method. here checked exception occur, which is always required to be handled while cloning an object. If object pass this check, than java.lang.Object's clone() method creates a shallow ans a
copy of object and returned it to the caller. Since Object class' clone() method creates copy by creating new instance, and then copying field-by-field, similar to assignment operator, it's fine for primitives and Immutable object, but not suited if your class contains some mutable data-structure.

coming to this program here we have 2 classes
in main class we created the object for engineer class .

so passed values get initialized with constructor in engineer class.
clone method always declared with protected access,it does not allow outer class function access.
it provides safety for internal data .

clone method defined with exception handling mechanism
in try block clone() called with super,
student.clone() method returns engineer objct to copy.
thn sop prints ,copy.rollnum=446

Posted by Maheshwari Natarajan    2014-12-13 07:40:05


Posted by Shubham Bansal    2014-12-13 09:18:52

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-12-14 01:50:38

© meritcampus 2019

All Rights Reserved.

Open In App