This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused About Interface Classes

 
masl ali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diagram:
==================================================
Constructors:
public ConsLoIF(ImageFile first, ILoIF rest) { }
public MtLoIF() {}
public ImageFile(String name, int width, int height, String kind) {}
==================================================
EXAMPLES
//Image samples
public ImageFile img1 = new ImageFile("dog", 300, 200, "jpg");
public ImageFile img2 = new ImageFile("cat", 200, 200, "png");
//List of Images
public ILoIF mylist = new ConsLoIF(img1,new ConsLoIF(img2, new MTLoIF()));
==================================================

Question : How can I extract the image objects from mylist?

Confusion: mylist is a ILoIF so I cannot get the first of the list because ILoIF is
an interface and not a ConsLolF.

THANK YOU
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
masl ali wrote:Question : How can I extract the image objects from mylist?

Impossible to say. Nothing you show says there is any way to do that in any of the classes you mention. We would need more details.

Confusion: mylist is a ILoIF so I cannot get the first of the list because ILoIF is
an interface and not a ConsLolF.

Well, the interface doesn't seem to contain a concept of 'first' so why would you think there is a way to get it from the interface?

I think you are confusing the generalization of 'coding to the interface' to mean 'whenever a class implements an interface, you should always refer to an instance of the class as the interface type.' This isn't true. In the case of ConsLoIF, it looks like you have some functionality in the class that does not exist in the interface. In this case you need to refer to the instance as the ConsLoIF type, rather than the interface type, when you need access to that unique functionality. Your interface should only be used when its more generic common functional definition is all that is required.

A more broad analysis of the issue might be that you do not abstract enough of the functionality into the interface definition. Perhaps it needs a method to get the image, and another method to get the next ILoIF node ('rest'). But maybe not, you know more about how these classes/instances are designed than I do.
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

By the way, I don’t think there is such a thing as an interface class. There are classes and there are interfaces and y ou can make classes to implement interfaces, but not both together.
 
masl ali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
I think you are confusing the generalization of 'coding to the interface' to mean 'whenever a class implements an interface, you should always refer to an instance of the class as the interface type.' This isn't true. In the case of ConsLoIF, it looks like you have some functionality in the class that does not exist in the interface. In this case you need to refer to the instance as the ConsLoIF type, rather than the interface type, when you need access to that unique functionality. Your interface should only be used when its more generic common functional definition is all that is required.

A more broad analysis of the issue might be that you do not abstract enough of the functionality into the interface definition. Perhaps it needs a method to get the image, and another method to get the next ILoIF node ('rest'). But maybe not, you know more about how these classes/instances are designed than I do.


Thank you for the explanation but I think I found a workaround. Type Casting. If I want to access the rest of ConsLoIF as a ConsLoIF all I have to do is (ConsLoIF)rest.
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a poor workaround.
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
masl ali wrote:Thank you for the explanation but I think I found a workaround. Type Casting. If I want to access the rest of ConsLoIF as a ConsLoIF all I have to do is (ConsLoIF)rest.

No, no, no. That is precisely what you don't want to do.

Indeed, any solution that starts with the words "I think I found a workaround..." is unlikely to be correct. EVER.

Winston

PS: I think those class names (ImageFile aside) are awful. Now maybe they were forced on you, but Java is meant to be descriptive, not cryptic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic