aspose file tools*
The moose likes Java in General and the fly likes Not understandable error message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not understandable error message" Watch "Not understandable error message" New topic
Author

Not understandable error message

Rudolf Pecinovsky
Greenhorn

Joined: Aug 18, 2003
Posts: 4
I have two interfaces:

I wanted to define next interface:

And the compiler complains:

error: clone() in Object cannot implement clone() in IShape
public interface IShapeFactory<T extends IShape & IColorable>
attempting to assign weaker access privileges; was public

I don't know, how to translate this message into an understandable language.
Why an Object should implement clone() in IShape?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Rudolf Pecinovsky wrote:I don't know, how to translate this message into an understandable language.
Why an Object should implement clone() in IShape?

Well, TBH, I'm not sure of the exact mechanics, but Object's clone() method is protected and is defined to throw CloneNotSupportedExcetion, so it may have something to do with that.

But the fact is that you don't need to include the method at all. The mere fact that IShape extends Cloneable makes any class that implements it implicitly Cloneable.

HIH

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Welcome to the Ranch
Only 11 years to first post.
Winston is right; because you declared clone() in that interface, the inherited clone() method needs public access, and it has protected access in Object. If you look in books like Effective Java™ (chapter 3) by Joshua Bloch, you will find advice not to use clone() if possible.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Don't define a factory method in an interface. Interface methods are instance methods and factory methods are usually static, so cannot be defined in an interface. At least not before Java8.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Winston Gutkowski wrote:But the fact is that you don't need to include the method at all. The mere fact that IShape extends Cloneable makes any class that implements it implicitly Cloneable.

Except that by including it, all IShape implementing classes are required to override clone and make it public, and get rid of the CloneNotSupportedException. Which should never have been a checked exception though...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Campbell Ritchie wrote:Don't define a factory method in an interface. Interface methods are instance methods and factory methods are usually static, so cannot be defined in an interface. At least not before Java8.

Except the interface is the factory. It doesn't create instances of its own interface (IShapeFactory) but instances of another (IShape).
Rudolf Pecinovsky
Greenhorn

Joined: Aug 18, 2003
Posts: 4
Campbell Ritchie wrote:Interface methods are instance methods and factory methods are usually static, so cannot be defined in an interface. At least not before Java8.


It is not true. The Simple Factory Methods are static, however standard factory methods from the Factory Method design pattern are instance methods. Typical factory method is e.g. iterator() method in Iterable interface.
Rudolf Pecinovsky
Greenhorn

Joined: Aug 18, 2003
Posts: 4
Campbell Ritchie wrote:Winston is right; because you declared clone() in that interface, the inherited clone() method needs public access, and it has protected access in Object.

No. Inherited method has public access, because parent method has public access. When you remove & IColorable in my example, everything works fine.

The problems start with the mentioned second parent type in the type parameter declaration. I think that compiler is afraid that the parent from the IColorable branch will not be able to behave correctly.

Campbell Ritchie wrote:If you look in books like Effective Java™ (chapter 3) by Joshua Bloch, you will find advice not to use clone() if possible.

I do not understand this problem as the problem of using clone(), but as a general problem of generic types inheritance. I wanted to understand it independently on clone() method. I believe that we can change this method for another one and with appropriate inheritance hierarchy the problem will appear again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not understandable error message