Re: (Probably) easy question about inheritance
On Aug 24, 11:04 pm, Zerex71 <Zere...@msn.com> wrote:
Group,
I'm wondering about the following: Say I have a class called Matrix
which is essentially an array of three numbers, and I call the data
member:
private double array[3];
to be used for general math matrix operations. However, I am going to
create a Vector class which subclasses from Matrix, since a vector is
a matrix. In it, I would like to access three data members:
private double x;
private double y;
private double z;
1. Is there a way to refer to x, y, and z when using Vector instead of
array[0..2]? In other words, when I say Vector.x, it automatically
reads/writes array[0]. Is this possible?
2. Is there a way that I can avoid duplicating data members between
instances of both classes?
3. Or in inheritance, will I just wind up with six doubles, and have
to deal with it?
Thanks,
Mike
Its a design issue actually. see,
when I say Vector.x, it automatically reads/writes array[0]. Is this possible?
When you want the members Vector.x,y,z 'read from and write into'
Matrix.array[0,1,2], why should you duplicate them in child class?
mean, why should you override it?
If the name x,y,z mandatory in Vector class, then I would suggest this
design.
class Matrix
{
protected double [] array = new double[3]; // change the scope to be
protected.
}
class Vector extends Matrix
{
//use getter and setters to access the parent class vars.
public double getX()
{
return array[0];
}
public double getY()
{
return array[1];
}
public double getZ()
{
return array[2];
}
public void setX(double x)
{
array[0] = x;
}
public void setY(double y)
{
array[1] = y;
}
public void setZ(double z)
{
array[2] = z;
}
}
--
Manivannan Palanichamy (@) Oracle.com
http://mani.gw.googlepages.com/index.html