aspose file tools*
The moose likes Beginning Java and the fly likes Confused About Interface Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused About Interface Classes" Watch "Confused About Interface Classes" New topic
Author

Confused About Interface Classes

masl ali
Greenhorn

Joined: Feb 18, 2013
Posts: 4
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

Joined: Jan 28, 2003
Posts: 4174
    
  21

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.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
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

Joined: Feb 18, 2013
Posts: 4
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

Joined: Oct 13, 2005
Posts: 38334
    
  23
That sounds like a poor workaround.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7636
    
  19

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.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused About Interface Classes