Query regarding java Multiple Inheritance

Discussion in 'Java' started by Amit Jain, Nov 27, 2009.

  1. Amit Jain

    Amit Jain Guest

    Hi All,
    I am new to Java and need your views for my query.

    What will be the solution for implementint two Interface in a single
    class A if these interfaces I1 and I2 are written as mentioned below:
    interface I1{
    void method ();
    long square(long i);
    }

    interface I2{
    String method();
    long squareRoot(long i);
    }

    class A implements I1, I2{
    void method(){
    System.out.println("--- Hi ----");
    }
    // implementation of square() and squareRoot() method
    }

    Above implementation of interface in class A will never complies
    because of "void method();" declared in both I1 and I2. And we want
    the implementation of I1 and I2 in class A only. How can we acheive
    this in such situations.

    regards,
    Amit J.
     
    Amit Jain, Nov 27, 2009
    #1
    1. Advertising

  2. Amit Jain

    Lew Guest

    Amit Jain wrote:
    > Hi All,
    > I am new to Java and need your views for my query.
    >
    > What will be the solution for implementint two Interface in a single
    > class A if these interfaces I1 and I2 are written as mentioned below:
    > interface I1{
    > void method ();
    > long square(long i);
    > }
    >
    > interface I2{
    > String method();
    > long squareRoot(long i);
    > }
    >
    > class A implements I1, I2{
    > void method(){
    > System.out.println("--- Hi ----");
    > }
    > // implementation of square() and squareRoot() method
    > }
    >
    > Above implementation of interface in class A will never complies
    > because of "void method();" declared in both I1 and I2.


    That is not a correct statement. 'void method()' is only declared in 'I1'.

    > And we want the implementation of I1 and I2 in class A only.
    > How can we acheive this in such situations.


    Your problem is that 'method()' has incompatible return types in the two
    interfaces, otherwise 'A' could happily implement both.

    An interface is a declaration of type. As such, it makes promises about the
    public face of any implementation. A class can satisfy the promises of any
    number of interfaces as long as those promises don't contradict each other.

    In your example, the promise made by 'I1' for 'method()' to return 'void'
    contradicts the promise in 'I2' to return 'String'. No way an implementation
    can keep both promises for the same-named method.

    To do what you want, you're either going to have to give the 'method()' method
    compatible return types in both interfaces, or change its name in at least one
    of them.

    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.5>
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8>

    --
    Lew
     
    Lew, Nov 27, 2009
    #2
    1. Advertising

  3. Amit Jain

    Arne Vajhøj Guest

    Amit Jain wrote:
    > Hi All,
    > I am new to Java and need your views for my query.
    >
    > What will be the solution for implementint two Interface in a single
    > class A if these interfaces I1 and I2 are written as mentioned below:
    > interface I1{
    > void method ();
    > long square(long i);
    > }
    >
    > interface I2{
    > String method();
    > long squareRoot(long i);
    > }
    >
    > class A implements I1, I2{
    > void method(){
    > System.out.println("--- Hi ----");
    > }
    > // implementation of square() and squareRoot() method
    > }
    >
    > Above implementation of interface in class A will never complies
    > because of "void method();" declared in both I1 and I2. And we want
    > the implementation of I1 and I2 in class A only. How can we acheive
    > this in such situations.


    Java does not allow this construct. You need a workaround.

    I once created the example below to illustrate a
    possible workaround which I believe is a reasonable good
    one.

    Arne

    ========================

    public class MultiInterf {
    public static void main(String[] args) {
    A o = new A();
    System.out.println(o.asB().f());
    System.out.println(o.asC().f());
    }
    }

    interface B {
    Object f();
    }

    interface C {
    boolean f();
    }

    class A {
    public Object fB() {
    return null;
    }
    public boolean fC() {
    return false;
    }
    public B asB() {
    return new AB(this);
    }
    public C asC() {
    return new AC(this);
    }
    private static class AB implements B {
    private A real;
    public AB(A real) {
    this.real = real;
    }
    public Object f() {
    return real.fB();
    }
    }
    private static class AC implements C {
    private A real;
    public AC(A real) {
    this.real = real;
    }
    public boolean f() {
    return real.fC();
    }
    }
    }
     
    Arne Vajhøj, Nov 27, 2009
    #3
  4. In article
    <>,
    Amit Jain <> wrote:

    > I am new to Java and need your views for my query.
    >
    > What will be the solution for implementint two Interface in a single
    > class A if these interfaces I1 and I2 are written as mentioned below:
    > interface I1{
    > void method ();
    > long square(long i);
    > }
    >
    > interface I2{
    > String method();
    > long squareRoot(long i);
    > }
    >
    > class A implements I1, I2{
    > void method(){
    > System.out.println("--- Hi ----");
    > }
    > // implementation of square() and squareRoot() method
    > }
    >
    > Above implementation of interface in class A will never complies
    > because of "void method();" declared in both I1 and I2. And we want
    > the implementation of I1 and I2 in class A only. How can we acheive
    > this in such situations.


    If the two methods named "method" are actually the same, derive A from
    an abstract class that implements I1 & I2. See "When an Abstract Class
    Implements an Interface":

    <http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html>

    For example,

    public class AbstractInterface {

    public static void main(String[] args) {
    A a = new A();
    System.out.println(a.method());
    System.out.println(a.square(2));
    System.out.println(a.cube(2));
    }

    interface I1 {
    long square(long i);
    }

    interface I2 {
    long cube(long i);
    }

    private static abstract class X implements I1, I2 {
    public String method() {
    return "Hi!";
    }
    }

    private static class A extends X {

    @Override
    public long square(long i) { return i * i; }

    @Override
    public long cube(long i) { return i * i * i; }
    }
    }

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 27, 2009
    #4
  5. Amit Jain

    Sarkar Guest

    Thanks all for valuable inputs. Now things are more clear to me.

    Thanks once again

    regards, Amit J.
     
    Sarkar, Nov 27, 2009
    #5
  6. John B. Matthews wrote:
    > In article
    > <>,
    > Amit Jain <> wrote:
    >> I am new to Java and need your views for my query.
    >>
    >> What will be the solution for implementint two Interface in a single
    >> class A if these interfaces I1 and I2 are written as mentioned below:
    >> interface I1{
    >> void method ();
    >> long square(long i);
    >> }
    >>
    >> interface I2{
    >> String method();
    >> long squareRoot(long i);
    >> }
    >>
    >> class A implements I1, I2{
    >> void method(){
    >> System.out.println("--- Hi ----");
    >> }
    >> // implementation of square() and squareRoot() method
    >> }
    >>
    >> Above implementation of interface in class A will never complies
    >> because of "void method();" declared in both I1 and I2. And we want
    >> the implementation of I1 and I2 in class A only. How can we acheive
    >> this in such situations.

    >
    > If the two methods named "method" are actually the same,


    They are not - they return different type.

    Arne
     
    Arne Vajhøj, Nov 28, 2009
    #6
  7. In article <4b107f0b$0$283$>,
    Arne Vajhøj <> wrote:

    > John B. Matthews wrote:
    > > In article
    > > <>,
    > > Amit Jain <> wrote:
    > >> I am new to Java and need your views for my query.
    > >>
    > >> What will be the solution for implementint two Interface in a single
    > >> class A if these interfaces I1 and I2 are written as mentioned below:
    > >> interface I1{
    > >> void method ();
    > >> long square(long i);
    > >> }
    > >>
    > >> interface I2{
    > >> String method();
    > >> long squareRoot(long i);
    > >> }
    > >>
    > >> class A implements I1, I2{
    > >> void method(){
    > >> System.out.println("--- Hi ----");
    > >> }
    > >> // implementation of square() and squareRoot() method
    > >> }
    > >>
    > >> Above implementation of interface in class A will never complies
    > >> because of "void method();" declared in both I1 and I2. And we want
    > >> the implementation of I1 and I2 in class A only. How can we acheive
    > >> this in such situations.

    > >
    > > If the two methods named "method" are actually the same,

    >
    > They are not - they return different type.


    Indeed, I was puzzled by the contradiction between the OP's statement
    '"void method();" declared in both I1 and I2' and the divergent return
    types you mention. My suggestion was intended to address the former
    interpretation; your class MultiInterf addresses the latter.

    I appreciate the OP responding, but I would enjoy hearing more about the
    resolution.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 28, 2009
    #7
  8. Amit Jain

    Roedy Green Guest

    On Thu, 26 Nov 2009 19:43:30 -0800 (PST), Amit Jain
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Above implementation of interface in class A will never complies
    >because of "void method();" declared in both I1 and I2. And we want
    >the implementation of I1 and I2 in class A only. How can we acheive
    >this in such situations.


    In Java, you can't have two methods that differ only in return type.
    There is no Eiffelian rename technique to resolve such conflicts. You
    have to manually rename the method in one of your interfaces.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I mean the word proof not in the sense of the lawyers, who set two half proofs equal to a whole one, but in the sense of a mathematician, where half proof = 0, and it is demanded for proof that every doubt becomes impossible.
    ~ Carl Friedrich Gauss
     
    Roedy Green, Nov 28, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,197
    Martijn van Steenbergen
    Dec 21, 2003
  2. darkstorm
    Replies:
    3
    Views:
    367
    Malte Starostik
    Mar 29, 2005
  3. Replies:
    6
    Views:
    294
    Chris Uppal
    Feb 9, 2007
  4. Rouslan Korneychuk
    Replies:
    8
    Views:
    621
    Rouslan Korneychuk
    Feb 10, 2011
  5. Jon F.

    CAML Query: Multiple Query Fields Issue

    Jon F., May 12, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    773
    Jon F.
    May 12, 2004
Loading...

Share This Page