4

I have a class Vector with a constructor

Vector(int dimension) // creates a vector of size dimension

I have a class Neuron that extends the Vector class

public class Neuron extends Vector {

    public Neuron(int dimension, ... other parameters in here ...) { 
         super(dimension);
         // other assignments below here ...
     }    
}

What I want to be able to do is assign the Vector in the Neuron class a reference to another Vector. Something along the lines of

    public Neuron(Vector v, ... other parameters in here ...) { 
         super = v;
         // other assignments below here ...
     }    

Of course, I can't do this. Is there some work around? Even if I was not able to do this in the constructor of the Neuron class, that would probably be OK.

COM
  • 847
  • 9
  • 23
  • 1
    Wanting to do this is often a good indication that you should be using [composition instead of inheritance](http://stackoverflow.com/a/2399554/228171) – Mark Peters Jul 26 '12 at 15:39
  • I wouldn't use Vector unless you have to. I would use ArrayList instead and I wouldn't sub-class it. It is better to use delegation as required. – Peter Lawrey Jul 26 '12 at 15:45
  • Vector is my own class that mimics the behavior of mathematical vectors. I should have made it clear that I wasn't talking about the java.util Vector class here. However, it's more of a general question on assigning a reference to the super class. – COM Aug 02 '12 at 04:13

1 Answers1

11

You need to create a copy constructor in the Vector class:

public Vector(Vector toCopy) {
    this.dimension = toCopy.dimension;

    // ... copy other attributes
}

and then in Neuron you do

public Neuron(Vector v, ... other parameters in here ...) { 
     super(v);
     // other assignments below here ...
}

You may also consider using on composition instead of inheritance. In fact, that is one of the recommendations in Effective Java. In such case you would do

class Neuron {
    Vector data;

    public Neuron(Vector v, ... other parameters in here ...) {
        data = v;
        // other assignments below here ...
    }
}

Related questions:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I implemented something like that and it didn't give me exactly what I was looking for. The dimension specifies the size of the array in the Vector class. The array is the only member of the class so this is what I have: 'public Vector(Vector toCopy) { this.array = toCopy.array }' However, when I use the copy constructor to do 'Vector v2 = new Vector(v1); System.out.println(v1); System.out.println(v2);' it prints out two different things which is different than v1 = v2. Though when I make a change to one vector it carries through to the other. – COM Jul 26 '12 at 15:50
  • As long as you do `this.array = toCopy.array` changes to one array should affect the other too. Just avoid things such as `System.arraycopy` and you should be fine. – aioobe Jul 26 '12 at 15:51
  • Interesting that you posted those links about composition vs inheritance because I was using composition before and switched to inheritance as it mad the code a bit cleaner, but maybe i should switch back. – COM Jul 26 '12 at 16:20
  • It usually looks a bit cleaner code-wise when changing to inheritance, but it's not as flexible and easy to maintain. The rule of thumb is to use inheritance, if and only if there is a true *is-a* relationship between the classes. So, ask yourself, *is* a Neuron a Vector? – aioobe Jul 26 '12 at 16:46