The Artima Developer Community
Sponsored Link

Java Answers Forum
Iterface method name?

2 replies on 1 page. Most recent reply: May 12, 2006 1:05 AM by Patrick He

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
john milton

Posts: 2
Nickname: johnm1997
Registered: May, 2006

Iterface method name? Posted: May 11, 2006 6:32 AM
Reply to this message Reply
Advertisement
If i have 2 intefraces named "Node".. everything is ok, they have diffrent package and i can still make:
Class MyClass implemets org.a.Node, com.b.Node

but, what if both interfaces declare function "int getSize()" ?

and for the first interface, i need to return 1, and the soncond- return 2?

is there no way to use two interfaces when they declare the same "function name" ?


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Iterface method name? Posted: May 11, 2006 6:48 AM
Reply to this message Reply
> is there no way to use two interfaces when they declare
> the same "function name" ?

You can implement both as long as the signatures are compatible, there's just no (good) way to return different values.

If you need to do this you can work around it using a wrapper or a closure.

Patrick He

Posts: 2
Nickname: patrickhe
Registered: Sep, 2005

Re: Iterface method name? Posted: May 12, 2006 1:05 AM
Reply to this message Reply
"if an interface inherits more than one method with the same signature, or if a class implements different interfaces containing a method with the same signature, there is only one such method. The implementation of this method is ultimately defined by the class implementing the interfaces, and there is no ambiguity there. If the methods have the same signature but different return types, then one of the return types must be a subtype of all the others, otherwise a compile-time error occurs. The implementation must define a method that returns that common subtype."

-- from Java Programming Language, 4th Edition : 4.3.2

Flat View: This topic has 2 replies on 1 page
Topic: Q about type conversion in Java 5 Previous Topic   Next Topic Topic: ending a loop with input?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use