0

I am having some trouble with my teacher, we have to master the concepts of java in aabout one month, he is saying that following is possible:

so in Java every class is inheriting from Object class, this class provides us with methods like protected Object clone() for example: Now let´s say that we have class Car

// later in main:

Car mycar=new Car();
//he is saying now that  following is possible:
Car yourCar=(Car) mycar.clone();

but every article online says that this is not possible, even when i try to compile it it's not possible, firstly because the method is protected and secondly because it would throw an exeption

is there something that I am missing ?

Harry
  • 101
  • 5
  • 2
    You have **not** posted the class `Car`; does it override `clone()` with a `public` signature? – Elliott Frisch Oct 25 '19 at 22:20
  • 1
    https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone-- "First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown". Please show the definition of `Car` –  Oct 25 '19 at 22:21
  • no he does not override it, thats the point thats why i am so confused – Harry Oct 25 '19 at 22:21
  • 1
    If he doesn't override the `clone()` method in the `Car` class, then this is not possible, you will receive those errors. – Bergis Oct 25 '19 at 22:22
  • Possible duplicate of [What is the use of cloneable interface in java?](https://stackoverflow.com/questions/11481552/what-is-the-use-of-cloneable-interface-in-java) – Progman Oct 25 '19 at 22:23
  • 1
    Well its simple enough: If `Car` implements `Cloneable` your teacher is correct, if it doesn't you will have the exception and your teacher is wrong. Tell him, Stackoverflow said that :) – Jorge Campos Oct 25 '19 at 22:28
  • I downvoted the question, because a lot of people waste their times on guesses, since we still don't see the `Car`'s definition. –  Oct 25 '19 at 22:31
  • @dyukha he didnt gave the car definition that was the whole example, he said later on we will learn how to implement it but for now we can just call the Method like this – Harry Oct 25 '19 at 22:33

2 Answers2

2

Yes, you can use .clone() without overriding it as long as the class implements Cloneable.

Here's an example:

class Car implements Cloneable {
  String name;
  public Car(String n) {
    name = n;
  }

  public static void main(String[] args) throws Exception {
    Car c1 = new Car("Lightning McQueen");
    Car c2 = (Car) c1.clone();
    System.out.println(c2.name);
  }
}

Here's the description of the default behavior of clone() when the class implements Cloneable but does not override clone():

this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • You don't have to override `clone()`, but you do have to obey the `protected` modified. Either call it from the class or a subclass, or hack it via reflection. – that other guy Oct 25 '19 at 22:32
  • but you made this call `(Car) c1.clone();` car is a subclass but is not in java.lang package, even though you implemented cloneable why is it that you can make the call shouldnt compiler say method not visible ? – Harry Oct 25 '19 at 22:39
  • 1
    `clone()` is a protected method in `Object`, so therefore it'll be inherited as a protected method in `Car extends Object`. Since it's called from within the class Car, this is fine. – that other guy Oct 25 '19 at 22:41
  • I would be verry thankfull if you could explain me the following: since we didnt override clone method how does the method knows what fields need to be copied? I mean car has the Attribute String :it would just copy the refernce but still how does it know that this Attribute is part of this class and needs to be copied ? – Harry Oct 25 '19 at 23:10
  • 1
    @Harry The Java platform has a [reflection API](https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) that you can use to programmatically list and access object members. It most likely uses this kind of functionality. – that other guy Oct 25 '19 at 23:15
1

Your teacher is correct if Car exposes an overriden implementation of public Object clone. Subclasses can widen the access to overriden methods, so it is possible for clone to have public access. You could also call the method within the class itself, even if you don't widen the access.

And clone will throw a CloneNotSupportedException if called on a object that hasn't overriden it:

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

Which explains why you're getting an Exception when you try. You need to call clone on an object that has implemented it.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117