Make a class use another class?

Hi I’m working on a math library and I got the Idea to make a Matrix where you can put anything in. The class you put in should have certain methods. I thougt about demanding it to extend a abstract Class Number I heard you could try something like.

class Matrix<Number>{
//Do stuff
}

I could of course try to make a interface and get the Methods there but the first option would be better.

1 Like

You are referring to Java Generic Types
It is worth to explore this concept further and see if it fits your use case.

Kf

3 Likes

In Processing documentation and examples, the most common place that generic types are used is with ArrayList, which is a Java built-in implemented with generics that works in exactly this way (ArrayList<Particle>, ArrayList<PVector>, ArrayList<PImage>, et cetera).

https://processing.org/reference/ArrayList.html

Here is the equivalent code from openjdk 8:

https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/ArrayList.java

1 Like

I’ve got this example of a class named ArrayQueue which is implemented using : :wink:
public class ArrayQueue<E> implements Queue<E>, Cloneable, Serializable {


1 Like