Results 1 to 8 of 8
 02022017, 03:43 PM #1Member
 Join Date
 Jan 2017
 Posts
 26
 Rep Power
 0
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; 02022017 at 03:57 PM. Reason: typo
 02022017, 04:10 PM #2Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
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 **
 02022017, 04:16 PM #3Member
 Join Date
 Jan 2017
 Posts
 26
 Rep Power
 0
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; 02022017 at 04:27 PM.
 02022017, 04:30 PM #4Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
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 **
 02022017, 04:33 PM #5Member
 Join Date
 Jan 2017
 Posts
 38
 Rep Power
 0
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?
 02022017, 05:03 PM #6Member
 Join Date
 Jan 2017
 Posts
 26
 Rep Power
 0
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; 02022017 at 05:13 PM.
 02022017, 07:10 PM #7Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
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> { }
And any type for which an operation is illegal could simply
display something like "illegal operation for type"
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 02022017, 07:52 PM #8Member
 Join Date
 Jan 2017
 Posts
 26
 Rep Power
 0
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> { }
I thank you for pointing me in the right direction.
Similar Threads

Have a question about Interfaces abstract classes and arraylists...
By Nyxeka in forum New To JavaReplies: 2Last Post: 03202012, 10:55 PM 
[Digital Signature] Embed P7S signature inside PDF with iText and BouncyCastle
By Ray1184 in forum Advanced JavaReplies: 1Last Post: 11022011, 09:39 AM 
abstract methodabstract class
By durgaprasad1407 in forum New To JavaReplies: 1Last Post: 04292011, 06:58 PM 
Class is not abstract and does not override abstract method run(com.
By rgeurts in forum New To JavaReplies: 4Last Post: 04142011, 11:42 AM 
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02112009, 10:51 AM
Bookmarks