Results 1 to 8 of 8
  1. #1
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Confused about signature in interfaces/abstract class

    Let's say I want an interface that implements arithmetic operations. I would write something like this:

    public interface Arithmetic {
    Arithmetic add(Arithmetic other);
    Arithmetic times(Arithmetic other);
    [ others methods like subtract, divide, etc... ]
    }

    so that if A,B are instances of a class implementing Arithmetic, I could write A.add(B). Now I define a class implementing Arithmetic, say Rationnal. But I seem to be obliged to write the add method in the Rationnal class like this:

    Arithmetic add(Arithmetic other) { ... }

    exactly as in the interface, while I would like to write:

    Rationnal add(Rationnal other) { ... }

    Is there a way to specify the type of the class that will implement the interface in the interface itself? What's the proper way to deal with this problem? Using Artithmetic in the signature of Rationnal may somehow work but does not seem very clean and will allow the method to be called with an argument of another class implementing arithmetic possibly incompatible with it. My final goal is to write a generic Matrix class of elements that implement Arithmetic and do computation with that (Linear algebra algorithms on matrices will work provided the elements supports enough arithmetics).
    Last edited by diolu; 02-02-2017 at 03:57 PM. Reason: typo

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Confused about signature in interfaces/abstract class

    That interface only makes sense if you can add one Arithmetic object to any other Arithmetic object.
    If you can't (and your example implies you can't) then your model doesn't work.

    I don't know (or remember) enough about matrices to come up with a model, but your description above shows to me that the one you are looking at doesn't fit the problem at hand.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Confused about signature in interfaces/abstract class

    You are right I can't add two arbitrary arithmetic objects. I can add two ojects implementing arithmetics if boths objects have exactly the same type: I can add two Rationals or, if StrangeNumber is another class implementing arithmetic, I can add two StrangeNumber. But I can't add a Rational and a StrangeNumber. What's the proper model for this? Maybe check the class at run type with the getClass() method and throw an Exception?
    Last edited by diolu; 02-02-2017 at 04:27 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Confused about signature in interfaces/abstract class

    So the interface is meaningless and should be ditched.
    It doesn't represent the reality.

    As I say, I can't give you more pointers as I don't know what is being modelled well enough.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Confused about signature in interfaces/abstract class

    I still don't get it what are you trying to do. Are you learning interfaces, or just operating on matrices?
    Why don't you just add methods you need inside the Matrix class?

    Or you want all math operations to be inside MathOps interface, so that you could use it with other math objects besides matrices?

  6. #6
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Confused about signature in interfaces/abstract class

    My ultimate goal is to define a generic Matrix like this:

    class Matrix<T extends Arithmetics> {
    ...
    }

    And compute the inverse, product, etc of matrices. I can't write the definition directly in the Matrix because the only thing I know is that my generic type T should support Arithmetics. This si true that learning interfaces is part of the job. Anyway, I come from Python which support so called "duck typing". In Python I can write a method that invert a Matrix completely generally only supposing that the +,-,*,/ will work on the elements of the Matrix. Exactly the same function will work for Matrix whose elements are floats, rationals, bigdecimals, complex, or whatever "numbers" you have in mind provided you have implemented arithmetical operations on the "numbers". Same for the determinant. I wonder if something like this is possible in java and what's the best way to achieve it.
    Last edited by diolu; 02-02-2017 at 05:13 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Confused about signature in interfaces/abstract class

    I would expect you would have something like this.

    Java Code:
    interface Operations<T> {
    public T add(T obj);
    public T mul(T obj);
    }
    
    class Complex implements Operations<Complex> {
       
    }
    class Matrix implements Operations<Matrix> {
    
    }
    Other interfaces could extend Operations for special types.
    And any type for which an operation is illegal could simply
    display something like "illegal operation for type"

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Confused about signature in interfaces/abstract class

    Your generic interface Operations seems what I need. However the Matrix instances do not support Operations it is the elements of the Matrix that must support Operations. However this Matrix header should work:

    Java Code:
    class Matrix<T extends Operations> {
        
    }
    and now I can take a Matrix of complex numbers: Matrix<Complex>
    I thank you for pointing me in the right direction.

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 10:55 PM
  2. Replies: 1
    Last Post: 11-02-2011, 09:39 AM
  3. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  4. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  5. Replies: 6
    Last Post: 02-11-2009, 10:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •