[ajug-members] correct interface programming
mcdaniel
521 at ofig.org
Thu Jul 24 21:16:39 EDT 2008
j.wilson; thanks- i'm learning from what you typed, you see my
point/question
barclay
Wilson, Jeff wrote:
> Here's some perfectly valid code:
>
> public interface Growable {
> double getGrowthRate();
> Dimension getCurrentSize();
> }
>
> public interface Shady {
> boolean isInShade(Point pt);
> }
>
> public interface LumberSource {
> double getPotentialBoardFeet();
> boolean isReadyToCut();
> }
>
> public class Tree extends Plant implements Growable, Shady, LumberSource {
> private double width;
> private double height;
>
> public Dimension getCurrentSize() { return new Dimension(width, height); }
> public boolean isInShade(Point pt) { return false; }
> public double getPotentialBoardFeet() { return 2000.0; }
> public boolean isReadyToCut() { return false; }
> }
>
> public class Plant {
> public double getGrowthRate() { return 0.42; }
> }
>
> No, Tree can implement any number of interfaces, there is no limit.
>
> Yes, Tree must have a method declaration for every method in all if the interfaces. HOWEVER, a method declared in a subclass of Tree with the same signature as a method in one of Tree's interfaces satisfies that contract. For example, in the code above, the getGrowthRate() method of the Growable interface is met by the Plant class. That is perfectly valid.
>
> Yes, by implementing an interface, this is a contract -- the compiler will force you to have a method for every method in an interface your class implements, either in the class or one of its parent classes.
>
>
>
> +jeff (MJW)
> ---
>
> "Foreploy: Any misrepresentation about yourself for the purpose of obtaining sex."
>
> ---------------------------------------------
> M. Jeff Wilson, Lead Member Technical Staff
> AT&T Services, Inc.
> Operations & Service Dev
> jw9615 at att.com
> +1 404.499.7235
>
>> -----Original Message-----
>> From: 521 [mailto:521 at ofig.org]
>> Sent: Thursday, July 24, 2008 3:43 PM
>> To: ajug-members at ajug.org
>> Subject: [ajug-members] correct interface programming
>>
>> am i saying this correctly?, that which is below... -barclay
>>
>> {{{
>> if i program to an interface, i do not have to worry about the number of
>> interfaces that are implemented, do i? but do i not have to use all methods
>> in an implemented interface? i do have to- it's a contract. correct? and
>> because you can only extend one class, you have no contract there. correct?
>> can someone see what my question is?
>>
>> a tree; a tree can grow, a tree can provide shade; and a tree can provide
>> lumber for table-tennis.
>>
>> so my tree class is designed...
>>
>> then if i am implementing a 'tree'- it depends on which implementation i use
>> thus have a contract to use certain methods
>> }}}
>>
>>
>> _______________________________________________
>> ajug-members mailing list
>> ajug-members at ajug.org
>> http://www.ajug.org/mailman/listinfo/ajug-members
>>
>
> *****
>
> The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622
>
>
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>
>
>
More information about the ajug-members
mailing list