Every class extends object but why it is not possible for
every object to invoke clone() method. ideally protected
methods should be accessible from sub classes. isn't it?

Answers were Sorted based on User's Feedback



Every class extends object but why it is not possible for every object to invoke clone() method. i..

Answer / aslam

Since the method is protected we can access clone method
only within the package or from subclasses outside the
package.

for ex:
class A{
public static void main(String arg[]){
new A().clone();
/* This is legal since class A is a subclass of Object and
we r accessing within the subclass*/

}
}

class B{
public static void main(String arg[]){
new A().clone();
/* Illegal since we r accessing it from outside object */
}
}

Is This Answer Correct ?    5 Yes 1 No

Every class extends object but why it is not possible for every object to invoke clone() method. i..

Answer / surendrababu koppula

clone() method must be called on only cloneable objects so sub class must implement Cloneable marker interface

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More Core Java Interview Questions

whats the purposr of using serialization?

6 Answers  


How do you add an element to a hashset in java?

0 Answers  


String Reverse in Java...!

5 Answers  


explain about method overloading and method overriding with difficult examples

4 Answers  


Define how destructors are defined in java?

0 Answers  


What is difference between static class and normal class?

0 Answers  


What is immutable class in java?

0 Answers  


What is array length in java?

0 Answers  


In Serialization, whether you will use Static variables?

3 Answers   HCL,


What is object data type?

0 Answers  


Can an interface implement another interface?

0 Answers  


Difference between process and thread?

0 Answers   HP,


Categories