I created a little example. Imagine I have two classes:
public class Neuron {
ArrayList<Neuron> neighbours = new ArrayList<>();
int value = 1;
public Neuron() {
}
public void connect(ArrayList<Neuron> directNeighbours) {
for (Neuron node : directNeighbours) {
this.neighbours.add(node);
}
}
}
and a class that inherits from Neuron:
public class SpecialNeuron extends Neuron {
int value = 2;
public SpecialNeuron() {
}
}
In my case, I want the inheritance in order to avoid a lot of "if object is special do something" stuff. However, when I am calling:
public static void main(String[] args) {
ArrayList<Neuron> neurons = new ArrayList<>();
Neuron a = new Neuron();
Neuron b = new Neuron();
Neuron c = new Neuron();
neurons.add(b);
neurons.add(c);
a.connect(neurons);
ArrayList<SpecialNeuron> special = new ArrayList<>();
SpecialNeuron d = new SpecialNeuron();
SpecialNeuron e = new SpecialNeuron();
special.add(d);
special.add(e);
a.connect(special); //Error
}
it is not possible to use a List(SpecialNeuron) for a List(Neuron) parameter. What is wrong with this call, and is there a proper way to solve that issue? Furthermore, I could do
ArrayList<Neuron> special = new ArrayList<>();
Neuron d = new SpecialNeuron();
Neuron e = new SpecialNeuron();
special.add(d);
special.add(e);
a.connect(special); //works fine
Which works, but denies any usage of functions from the SpecialNeuron class.