Mr.Gomer
Mr.Gomer

Reputation: 649

Using methods defined in a interface to call the clone method

So to keep it short and simple i have a list of shapes List these are all shapes (Circle, square ect) that implement my Shape interface.

Now im trying to deep clone/copy every shape on this list and place it in. a new list. I have made all the shapes implement Clonable and made my own methods for making deeps copies of them.

The problem is i cant reach the clone method when they are simply defined as Shapes. My question is how do i get a hold of the clone method without having to cast them into concrete shapes.

I have been thinking about it and googling it but i cant seem to find a proper answer. And so i got an idea.

could i add a method "void callCloneMethod();" in my shapes interface and have the implementation call the clone() method of each concrete shape? IOW can a interface implemented method call non interface implemented methods. ?

Thank you in advance.

Upvotes: 0

Views: 61

Answers (1)

TheThingAboveMe
TheThingAboveMe

Reputation: 31

Did you try to let your Shape interface extend the Cloneable interface? This should allow you to call the method for any Shape, if i am not mistaken. You should then of course implement the clone method. Hope this helps.

Upvotes: 1

Related Questions