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

What is ternary operator? Give an example.

0 Answers  


What all access modifiers are allowed for top class ?

0 Answers  


explain copyonwritearraylist and when do we use copyonwritearraylist?

0 Answers  


How many unicode characters are there?

0 Answers  


what is the difference between String s="hello"; and String s=new String("hello");?

3 Answers  






What is the difference between actual and formal parameters?

0 Answers  


What are the OOPS concepts in Java ?

4 Answers   Satyam,


What design pattern you have used in your project? I answered Factory pattern, how it is implemented? What are its advantage? Do know about Abstract Factory?

0 Answers   Bravura Solutions,


How to connect to a remote database using Applet?

0 Answers  


What do negative exponents mean?

0 Answers  


How to solve the problem of generating the unique hash keys with hash function?

0 Answers  


What is actual difference between statement,prepared statement and callable statement and when we have to use it? pls post a answer with code and clear explanation. thanks

2 Answers  


Categories