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

Can bool be null?

0 Answers  


What are the differences between the constructors and methods?

0 Answers  


How will you load a specific locale?

0 Answers  


what is instanceof operator used in java?

0 Answers  


What is Java Classloader?

1 Answers   Phantom Technologies,






Why you should not use singleton?

0 Answers  


Is java programming easy?

0 Answers  


Differentiate Vector and ArrayList?

6 Answers   Max Telecom, Wipro,


How do you create a null object?

0 Answers  


What is a heavyweight component?

0 Answers  


How do you read and print a string in java?

0 Answers  


What are the performance implications of interfaces over abstract classes?

1 Answers  


Categories