Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not understandable error message

 
Rudolf Pecinovsky
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50284
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50284
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic