Build Fehler - PieceBuild is not abstract and does not override abstract method clone()

Error while compiling with exit status 1
src/tetris/game/pieces/PieceBuild.java:3: error: PieceBuild is not abstract and does not override abstract method clone() in Piece
public class PieceBuild implements Piece {
       ^
src/tetris/game/pieces/PieceBuild.java:21: error: clone() in PieceBuild cannot implement clone() in Piece
    protected Object clone() throws CloneNotSupportedException {
                     ^
  attempting to assign weaker access privileges; was public
2 errors
public class PieceBuild implements Piece {

Hi, I just couldn’t get rid of that Build-Fehler even though I changed somethings.
Can you explain what can be wrong here?

Thank you.

Oguzhan

Hi,

looks like your method was protected, but should be public. You should change the access modifier of the method to fix it.

best wishes
Lisa :smiley:

Hi Lisa thank you for taking your time to answer. But when I use protected then I face this error on clone().

Cannot reduce the visibility of the inherited method from Piece

I still believe using public makes more sense.

The method clone in the Piece interface has the access modifier public but your implementation of said method has the access modifier protected when it should be public as well. (That is what I read from the error message, I might be off)

A short google search yields a stack exchange question linking to this rule from the JLS:

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:

  • If the overridden or hidden method is public , then the overriding or hiding method must be public ; otherwise, a compile-time error occurs.

Just out of curiosity, what is the signature of your clone method in PieceBuild? If it is the second signature from the error message then something is probably wrong.

1 Like

Yes, @Bastian.Heinen is right, sorry for the confusion, I swapped public and protected in my answer, too. (It is not that easy sometimes…:slight_smile: ) However, I edited my post that it should be correct now.

Additionally, you seem to define Object as the return type of your function, but by looking at the signature at the interface, one can see, that the return type should be Piece. Therefore you are not overriding your method but overloading it.

You are not even doing this. You are just causing a type error, since the method from the child class must have the same (or more precise) return type as the method from the parent.

This makes sense because if the parent class has a method that only returns Dog, and if the subclass is then suddenly able to return any Animal, e.g. a Cat, you get a problem, since you might want to call Dog#bark which is of course impossible on a Cat, since cats are not dogs. Yet the method promised that we only get dogs.

Same with the visibility modifier (i.e. protected). You can not suddenly make the method less visible, since if you can call it on the parent class, and override it in the subclass, then calls that were permissible on the parent class must always be possible on the subclass. If your class Animal has a public void feed() method, you must be able to call it on any Animal. You can’t make it private in Dog since it must work on any Animal – otherwise, what happens if you invoke a.feed(); where a has static type Animal and dynamic type Dog?

That’s what the error message is telling you.

1 Like