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

Why are the methods of the math class static?

0 Answers  


According to java operator precedence, which operator is considered to be with highest precedence?

0 Answers  


what is inner class in java?

0 Answers   IBS,


Read data from console and print in one file. That would be in C:\temp ? Thanks, Bose

3 Answers   Oracle, Surya Software,


What do you mean by access modifier?

0 Answers  






What are different types of references?

0 Answers  


Which object oriented concept is achieved by using overloading and overriding?

0 Answers  


What are keywords give examples?

0 Answers  


Which is the best way of exception handling?

2 Answers   Oracle,


what is platform independence in java?

5 Answers   CSC, HSBC,


Is string a class in java?

0 Answers  


How to convert a string to long?

2 Answers  


Categories