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 a control variable example?

0 Answers  


What is the difference between a switch statement and an if statement?

0 Answers  


How many types of modifiers are there?

1 Answers   Infosys,


How can you set an applet’s height and width as a percentage?

0 Answers  


What is an object in Java and what are its benefits?

4 Answers   IBM,






what is polymorphism with example?types of polymorphism?

15 Answers   HP, Sigma Solve,


If a multi threaded Java program has started numerous number of threads, at any point in time how to know which thread is currently executing/running ?

7 Answers   Accenture, College School Exams Tests, iFlex, NIIT,


I want to run a simple hello world java (HelloWorld.java) program using a batch file. How can i run it and how to construct a batch file.

1 Answers   Infosys,


What is re-factoring in software?

0 Answers  


How do you use compareto method?

0 Answers  


difference throws and throw in java

3 Answers  


What is difference between classpath and path variables in java?

0 Answers  


Categories