I'm trying to write an abstract class that is dependent on some values to setup various parameters. I want to implement subclasses that set these critical values (amongst other things) that makes the super class construct the object in the required manner.
I can have my sub classes pass these values into the constructor of the base class, but I also want to be able to look at these value statically also.
Let me try and explain what I want to do with a simple example.
Lets say you have an abstract class Polygon and you want to extend it to Rectangle and Triangle.
class Polygon should have these methods:
getName() -- returns the name of the shape, i.e. Rectangle, Triangle
getNumPoints() -- returns the number of points needed to represent one of these polygons.
So I could define polygon like so:
public abstract class Polygon{
String name;
int numPoints;
Polygon(String name, int numPoints){
this.name = name;
this.numPoints = numPoints;
}
String getName(){
return name;
}
String getNumPoints(){
return numPoints;
}
}
Then, define Rectangle and Triangle as:
public class Rectangle extends Polygon{
Rectangle(){
super("Rectangle", 4);
}
}
public class Triangle extends Polygon{
Triangle(){
super("Triangle", 3);
}
}
The code above works for me but does not let me do a getName() and getNumPoints() statically which I should be able to do. Also, having each instance have these class parameters contributes to some memory overhead which I don't mind since I'm not going to have a huge number of these objects around at a time.
I know that I can't have abstract static methods that I could implement in each child class that would return the values I want.
I also need to have a factory that can generate these classes, so I was thinking of passing a unique key (string/int whatever) to the super class that could ask the factory for these parameters based on the unique key. The factory could then return these values statically.
Is there a better solution than this? Know of any good 'design patterns' that may help me out? Any links/resources would be a great help.
Thanks!
I can have my sub classes pass these values into the constructor of the base class, but I also want to be able to look at these value statically also.
Let me try and explain what I want to do with a simple example.
Lets say you have an abstract class Polygon and you want to extend it to Rectangle and Triangle.
class Polygon should have these methods:
getName() -- returns the name of the shape, i.e. Rectangle, Triangle
getNumPoints() -- returns the number of points needed to represent one of these polygons.
So I could define polygon like so:
public abstract class Polygon{
String name;
int numPoints;
Polygon(String name, int numPoints){
this.name = name;
this.numPoints = numPoints;
}
String getName(){
return name;
}
String getNumPoints(){
return numPoints;
}
}
Then, define Rectangle and Triangle as:
public class Rectangle extends Polygon{
Rectangle(){
super("Rectangle", 4);
}
}
public class Triangle extends Polygon{
Triangle(){
super("Triangle", 3);
}
}
The code above works for me but does not let me do a getName() and getNumPoints() statically which I should be able to do. Also, having each instance have these class parameters contributes to some memory overhead which I don't mind since I'm not going to have a huge number of these objects around at a time.
I know that I can't have abstract static methods that I could implement in each child class that would return the values I want.
I also need to have a factory that can generate these classes, so I was thinking of passing a unique key (string/int whatever) to the super class that could ask the factory for these parameters based on the unique key. The factory could then return these values statically.
Is there a better solution than this? Know of any good 'design patterns' that may help me out? Any links/resources would be a great help.
Thanks!