The Artima Developer Community
Sponsored Link

Java Answers Forum
Interfaces

8 replies on 1 page. Most recent reply: May 7, 2010 10:33 PM by Ramesh K

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 8 replies on 1 page
Ranjit Radhakrishnan

Posts: 7
Nickname: ranju4all
Registered: Aug, 2009

Interfaces Posted: Aug 12, 2009 1:07 PM
Reply to this message Reply
Advertisement
Hi, Could someone help me on this.

Interface A { String test(): }
Interface B { int test():}

I want an Interface C to implement both A and B. How can I do it programatically in java.


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Interfaces Posted: Aug 12, 2009 11:43 PM
Reply to this message Reply
> Hi, Could someone help me on this.
>
> Interface A { String test(): }
> Interface B { int test():}
>
> I want an Interface C to implement both A and B.
> How can I do it programatically in java.

An Interface cannot implement an Interface. Do you mean Class C to implement both A and B? If you are talking extension, Java does not allow for Multiple Inheritance. One interface can extend the next one but not both.

Ranjit Radhakrishnan

Posts: 7
Nickname: ranju4all
Registered: Aug, 2009

Re: Interfaces Posted: Aug 12, 2009 11:52 PM
Reply to this message Reply
Sorry for the confusion. I want a Class C to implement both A & B

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Interfaces Posted: Aug 12, 2009 11:53 PM
Reply to this message Reply
> Hi, Could someone help me on this.
>
> Interface A { String test(): }
> Interface B { int test():}
>
> I want an Interface C to implement both A and B.
> How can I do it programatically in java.

An Interface cannot implement an Interface. Do you mean Class C to implement both A and B? If you are talking extension, Java does not allow for Multiple Inheritance. One interface can extend the next one but not both.

Ranjit Radhakrishnan

Posts: 7
Nickname: ranju4all
Registered: Aug, 2009

Re: Interfaces Posted: Aug 12, 2009 11:57 PM
Reply to this message Reply
How do i achieve or could i achieve something like

public Class C implements A, B ?

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Interfaces Posted: Aug 13, 2009 12:59 AM
Reply to this message Reply
> Sorry for the confusion. I want a Class C to implement
> both A & B

Sorry for the double post - hit refresh.

public class C implements A, B{
}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Interfaces Posted: Aug 13, 2009 1:51 AM
Reply to this message Reply
> Hi, Could someone help me on this.
>
> Interface A { String test(): }
> Interface B { int test():}
>
> I want an Interface C to implement both A and B.
> How can I do it programatically in java.

You cannot implement two methods in the same class with the same signature. These two methods have the same signature. The return type is not part of the signature, just the method name and parameter list. Therefore, there is no way to achieve what you want, without changing one or both of the method names (or changing the return types so that both the test methods return the same type).

public interface A { String testA(); }
 
public interface B { int    testB(); }
 
public interface C extends A, B {}
 
public class D implements C // (or implements A, B (as C is not needed))
{
    @Override public String testA() { return "1"; }
    @Override public int    testB() { return 1;   }
}
or
public interface A { int test(); }
 
public interface B { int test(); }
 
public interface C extends A, B {}
 
public class D implements C   // (or implements A, B (as C is not needed))
{
    @Override public int test() { return 1; }
}

Ranjit Radhakrishnan

Posts: 7
Nickname: ranju4all
Registered: Aug, 2009

Re: Interfaces Posted: Aug 18, 2009 9:36 PM
Reply to this message Reply
Is something like this also apt?


public class Test {
public static void main(String[] args) {

}
public void operation()
{
TestOne testOne = this.new TestOne();
testOne.test();
}
public class TestOne implements B
{
public int test() {
return 0;
}
}

public class TestTwo implements A
{
public String test() {
return null;
}

}

}


Interface A

public interface A { int test(); }


Interface B

public interface B { String test(); }


Thanks
Ranjit

Ramesh K

Posts: 3
Nickname: ramesh009
Registered: Apr, 2010

Re: Interfaces Posted: May 7, 2010 10:33 PM
Reply to this message Reply
its not possible to implement two interfaces with having two method names same and return types are different if both return types are same then its enough to implement to one method but in your case two methods are having different return types..

Flat View: This topic has 8 replies on 1 page
Topic: Migration To Linux Previous Topic   Next Topic Topic: Need help in java

Sponsored Links



Google
  Web Artima.com   

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