1

Every class in java implicitly extends Object class.clone() is protected in Object. protected method is visible for child classes know.then why we need to override? i can't understand these concepts even i read many sites. please explain clearly. Thanks.

Priscilla Jobin
  • 609
  • 7
  • 19
Rose
  • 331
  • 2
  • 4
  • 8

3 Answers3

2

This is because then clone will be visible from the child class only. Other classes, even classes from the same package, will not see clone in child and thus will not be able to clone the child:

class Test2 implements Cloneable {
}

public class Test1 {


    public static void main(String[] args) throws Exception {
        Test2 t2 = new Test2();
        t2.clone(); <-- compile time error: Method clone() from type Object is not visible
    }
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Protected method will be applicable to child class right.All classes extends Object class in java.so,it should be visible know? – Rose Jul 02 '13 at 11:48
  • see example in my update – Evgeniy Dorofeev Jul 02 '13 at 11:57
  • As per your example Test2 extends Object(internally).So now Test2 will have protected clone method which is inherited from Object class.If I create object in Test1 class(in same package) for Test2 ,it should have access to protected members which in same package?(Test2 has protected clone() method).Why we are getting compile error. – Rose Sep 03 '13 at 12:42
  • No, it's visible only from class Test1 itself, classes in the same package cannot see it. You need to explicitly add protected clone() to Test1 to make it visible from classes in the the same package – Evgeniy Dorofeev Sep 03 '13 at 12:52
1

You only need to override those methods of superclass in which you need your own implementation. If you want the default behavior of superclass then no need to override

stinepike
  • 54,068
  • 14
  • 92
  • 112
1

Let's go over cloning step by step:

  • Although, every object inherits Object.clone(), cloning is not ON by default. That means if you try to clone() some random object you would probably get a CloneNotSupportedException.

  • To enable cloning, an object's class or one of it's super classes must implement Cloneable interface. It's just a marker interface without which the exception mentioned above would get thrown.

  • Assuming cloning has been enabled, the default implementation is inherited as protected. So, the class can still use it to create its object's copies but it cannot be called by the public yet.

  • Now, if your class wants to provide for making copies of its objects from outside of the class it can override Object.clone() as public and simply call super.clone() inside to still use the default implementation.

  • Which brings me to the last point. The default implementation is to make a shallow-copy of the object which means it would simply copy all the primitives as well as reference values on to the new object. This means, complex data structures, like a Map would get shared since both the copies though would have two different references but they would be pointing to the same Map.

    This is why an override most of the times has to provide its own implementation to make (what is better known as) a deep-copy of the object so that the clone is not sharing any state from its source object and can be modified independently without affecting other's state.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89