P
Paul O'Grady
I have two classes which are wrappers for a (1D or 2D) array of
primitives (one type only). I want to unify these classes (because they
only differ in the data type stored), by the provision of an abstract
base class (Parent).
class A extends Parent{
float [] data ;
...
}
class B extends Parent {
int [] data ;
...
}
abstract class Parent {
private int numRows = 0 ;
private int numCols = 0 ;
/* Methods */
public abstract void getItem(int Row) throws InvalidRowException,
InvalidColException ;
public abstract void getItem(int Row, int Col) throws
InvalidRowException, InvalidColException ;
public abstract void setItem(int Row) throws InvalidRowException,
InvalidColException, InvalidValueException ;
public abstract void setItem(int Row, int Col) throws
InvalidRowException, InvalidColException, InvalidValueException ;
public abstract void copy() ;
public abstract void reSize(int addNumRows) ; //increase 1D or 2D array
size by adding addNumRows new rows
public abstract boolean isEmpty() ;
public abstract boolean contains() ;
public abstract int findItemPos() ;
public abstract void sort(int sortType) ; //For now, only sort for 1D arays
public int getNumRows()
{
return numRows ;
}
public int getNumCols()
{
return numCols ;
}
}
The problem is that the getItem() functions will return different types,
and the setItem() function will accept different types. I thought I
could get around this by overloading and overiding the methods
respectively. Howewer, when I attempt to compile this code, I get an
error saying the return types are incompatable. Ditto with the Copy
method. What am I doing wrong?
Tks
primitives (one type only). I want to unify these classes (because they
only differ in the data type stored), by the provision of an abstract
base class (Parent).
class A extends Parent{
float [] data ;
...
}
class B extends Parent {
int [] data ;
...
}
abstract class Parent {
private int numRows = 0 ;
private int numCols = 0 ;
/* Methods */
public abstract void getItem(int Row) throws InvalidRowException,
InvalidColException ;
public abstract void getItem(int Row, int Col) throws
InvalidRowException, InvalidColException ;
public abstract void setItem(int Row) throws InvalidRowException,
InvalidColException, InvalidValueException ;
public abstract void setItem(int Row, int Col) throws
InvalidRowException, InvalidColException, InvalidValueException ;
public abstract void copy() ;
public abstract void reSize(int addNumRows) ; //increase 1D or 2D array
size by adding addNumRows new rows
public abstract boolean isEmpty() ;
public abstract boolean contains() ;
public abstract int findItemPos() ;
public abstract void sort(int sortType) ; //For now, only sort for 1D arays
public int getNumRows()
{
return numRows ;
}
public int getNumCols()
{
return numCols ;
}
}
The problem is that the getItem() functions will return different types,
and the setItem() function will accept different types. I thought I
could get around this by overloading and overiding the methods
respectively. Howewer, when I attempt to compile this code, I get an
error saying the return types are incompatable. Ditto with the Copy
method. What am I doing wrong?
Tks