aspose file tools*
The moose likes Beginning Java and the fly likes the first code on interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the first code on interface" Watch "the first code on interface" New topic
Author

the first code on interface

frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
Hi, please could you evaluate the following training code (the first one on interface)?
To keep the classes clear, I put all the printing stuff to a separate class Print - this was my intention.
The code is a bit larger but easy to read, I hope.

I don't like two things there, please give me advice on them.
1. these "returns 0" - I have no knowledge how to treat the methods which are not applicable for particular shape
2. when I want to create another shape I will have to also add appropriate code block to the print method

If the concept is totally stupid, just tell me.

Thanks a lot.



[ October 03, 2008: Message edited by: frantisek nejedly ]
[ October 03, 2008: Message edited by: frantisek nejedly ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
I don't like two things there, please give me advice on them.
1. these "returns 0" - I have no knowledge how to treat the methods which are not applicable for particular shape
2. when I want to create another shape I will have to also add appropriate code block to the print method

#1 - Instead of returning 0 you could return a negative value, clearly indicating that it is not a valid value. You could also throw an exception.

#2 - The print method should be part of the Shape interface, and thus be implemented by each class implementing the interface.


Ping & DNS - my free Android networking tools app
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Yep thats a good idea about putting the print on the Shape interface, in that way each shape knows how to print itself (similar to toString())and you dont need instanceof (or the Print class really).

Possibly you could introduce another interface(s) that extends Shape and move some of the shape specific into that. You would leave print(), and any other truly generic method in the top level Shape interface and have something like RoundShape, RectangularShape etc.

HTH


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
#1 - Instead of returning 0 you could return a negative value, clearly indicating that it is not a valid value. You could also throw an exception.

I see.
a - Can be the option of returnig negative value from NA methods considered as a normal and valid solution in this situation?
b - What if the negative return value is a valid one (of course not for shapes here)?



#2 - The print method should be part of the Shape interface, and thus be implemented by each class implementing the interface.

I see. I will play with it using Tom's suggestions.

Thank you very much.
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
You could throw an UnsupportedOperationException. I think though you should try to code it so they cannot be called on an object where it doesnt make sense....
frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
I understand, I haven't learnt exceptions yet, regarding this, could be the code, as it is, considered as an acceptable one, including standalone Print class?
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Well I wouldn't claim to be an expert on what code "should" look like....but its not bad at all. You're using interfaces in the calling code which is good, the Print class does what it says on the tin, just printing.

The main problem as we said is that if you add a new shape you need to remember to update Print class to handle it, whereas if the print method was on the Shape interface you would be forced to implement it in the new Shape class by the compiler, thereby preventing "Oh I didnt know the Print class did that" errors.
frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
Thanks a lot Tom.
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
No bother
frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
Did you mean something like this?


[ October 04, 2008: Message edited by: frantisek nejedly ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Note that if you have to implement interface methods by using 'return 0' or something like that, you should take it as a suggestion that either the class isn't really an instance of what the interface defines, or the interface itself is badly designed.

In this case, there's some of both: a "Shape" is generally a two-dimensional figure, so Spheres and Blocks shouldn't be shapes. A Shape, being two-dimensional, should have a height; and since "Radius" is a property of only one kind of Shape, it shouldn't be a property of the Shape interface.

Consider a design where "Shape" had only getArea() and print() methods, and where "Triangle" and "Trapezoid" replaced "Cube", "Block", and "Globe". M ethods like getRadius() and getWidth(), etc, belong only to the classes they apply to.


[Jess in Action][AskingGoodQuestions]
frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
final version, no 'return 0' needed


[ October 04, 2008: Message edited by: frantisek nejedly ]
[ October 04, 2008: Message edited by: frantisek nejedly ]
 
 
subject: the first code on interface