The Artima Developer Community
Sponsored Link

Java Answers Forum
Programming problem

7 replies on 1 page. Most recent reply: Jan 27, 2006 11:34 AM by Makhmud Kasumov

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 7 replies on 1 page
Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Programming problem Posted: Jan 18, 2006 11:44 PM
Reply to this message Reply
Advertisement
Hi all!

Here's my problem. I've got an interface that looks something like this:


public interface A
{
public A getInterface();
}


And I need to create another interface, which extends the previous one and has the same method with the same parameters list, except the return value type:


public interface B extends A
{
public B getInstance();
}


I want the interface B to have the same getInstance method to return a link to interface B, which, in its turn, extends interface A. But I get compile-time error. As I understand I cannot have getInstance() overriden like this, even if B extends A.

Is there any other way to achieve my goal?

Thank you?


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Programming problem Posted: Jan 19, 2006 12:08 AM
Reply to this message Reply
> Hi all!
>
> Here's my problem. I've got an interface that looks
> something like this:
>
>
> public interface A
> {
> public A getInterface();
> }
>

>
> And I need to create another interface, which extends the
> previous one and has the same method with the same
> parameters list, except the return value type:
>
>
> public interface B extends A
> {
> public B getInstance();
> }
>

...
>
> Is there any other way to achieve my goal?
>
> Thank you?
public interface B extends A{ 
//do nothing...
}

Because B is a sub-interface of A when you call getInstance
from a class that implements B, it will return a type-B
object by inheritance. You may actually check this
by running a print statement when you call this method:
if(someObj.getInstance() instanceof B){
    System.out.println("It is an instance of B");
}else{
    System.out.println("It is not an instance of B");
}

Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Re: Programming problem Posted: Jan 19, 2006 2:02 AM
Reply to this message Reply
>
> public interface B extends A{ 
> //do nothing...
> }
> 

> Because B is a sub-interface of A when you call
> getInstance
> from a class that implements B, it will return a type-B
> object by inheritance. You may actually check this
> by running a print statement when you call this method:
>
> if(someObj.getInstance() instanceof B){
>     System.out.println("It is an instance of B");
> }else{
>     System.out.println("It is not an instance of B");
> }
> 


Fine. I have already considered this solution. The way you proposed involves casting A to B. But this does not give me exactly what I need. I want to use the returned B-interface link further (that is, on the same line of code) to call other B-specific methods. The following code explains what I mean:

public interface A {
    public A getInstance();
}
 
public class B extends A {
    public B getInstance();
    public void specificMethod();
}
 


And then:

B b = new B();
b.getInstance().specificMethod();


Am I clear?

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Programming problem Posted: Jan 19, 2006 5:37 AM
Reply to this message Reply
I just compiled your initial examples it compiles perfectly
on my machine. I'm using Java 5.

Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Re: Programming problem Posted: Jan 19, 2006 7:10 AM
Reply to this message Reply
Please tell me where can I download it from? And what version Java 5 is meant?
Thank You!

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Programming problem Posted: Jan 19, 2006 10:34 PM
Reply to this message Reply
try the command

java -version

It will tell you what Java version you are running.

Java 5 is Java version 1.5

1.4 will probably also compile that code so you must be
running a very old JDK.

Go to the Sun Website and update because from what I've
noticed most of the responses on this site are usually
with the assumption that you have already update to either
1.4 or 1.5

Sanjeev Chakravarty

Posts: 2
Nickname: sanjeevc
Registered: May, 2004

Re: Programming problem Posted: Jan 20, 2006 7:32 PM
Reply to this message Reply
> Hi all!
>
> Here's my problem. I've got an interface that looks
> something like this:
>
>
> public interface A
> {
> public A getInterface();
> }
>

>
> And I need to create another interface, which extends the
> previous one and has the same method with the same
> parameters list, except the return value type:
>
>
> public interface B extends A
> {
> public B getInstance();
> }
>

>
> I want the interface B to have the same getInstance method
> to return a link to interface B, which, in its turn,
> extends interface A. But I get compile-time error. As I
> understand I cannot have getInstance() overriden like
> this, even if B extends A.
>
> Is there any other way to achieve my goal?
>
> Thank you?
>
>
I guess you meant getInstance() for both the interfaces or else you wouldn't have gotten the compile error. Java doesn't allow to override a method with same signature but different return type. For further details you may want to read the spec: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#227768

Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Re: Programming problem Posted: Jan 27, 2006 11:34 AM
Reply to this message Reply
> I just compiled your initial examples it compiles
> perfectly
> on my machine. I'm using Java 5.

Thanks! I installed Java 5 and now it compiles really perfectly!

Thanks again, you practically saved my project!

Flat View: This topic has 7 replies on 1 page
Topic: Problem with inter-applet communication Previous Topic   Next Topic Topic: File Server

Sponsored Links



Google
  Web Artima.com   

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