"Ovid" <pu**********************@yahoo.com> wrote in message
news:14**************************@posting.google.c om...
Hi,
I'm trying to determine the cleanest way to override class
data in a subclass.
class Universe {
public String name;
private static double PI = 3.1415;
Universe(String name) {
this.name = name;
}
public String toString() {
return "Universe: " + name + " PI: " + PI;
}
}
class UniverseRoman extends Universe {
private static double PI = 3;
UniverseRoman(String name) {
super(name);
}
public String toString() {
return "Universe: " + name + " PI: " + PI;
}
}
class TestUniverse {
public static void main(String args[]) {
Universe bob = new Universe("Bob");
UniverseRoman ovid = new UniverseRoman("Ovid");
System.out.println(bob);
System.out.println(ovid);
}
}
From what I can tell, if I want to override a class variable in a
subclass, I have to duplicate all methods that access the class
variable. If I want to be able to change the class data:
public static void setPI(double pi) {
PI = pi;
}
If I just provide that in my Universe class, calling
UniverseRoman.setPI(4) will set the Universe class PI to
4, not the roman value of pi. Duplicating that method in
UniverseRoman seems to be the answer, but part of the value
of OO is that, in theory, we shouldn't have to duplicate this
much code. What am I missing?
You seem to want 'virtual data, that is, to have a particular base class
field object replaced by sub-classes. The concept is quite similar to having
sub-classes override base class methods except that, by replacing a whole
object, you have - effectively - supplied a whole set of overridden methods,
those of the replaced object.
Depending on your needs, simply overriding a method might be far easier.
Now, at least, you have the choice :) !
I hope this helps.
Anthony Borla
P.S. If it isn't already clear to you a static method belongs to the base
class, and any of its derived classes. Such methods [or data for that
matter] are *not* overridable !
// ------------------------------------------------------
public class TestVirtualData
{
public static void main(String[] args)
{
System.out.println("PI in ThisUniverse is: "
+ new ThisUniverse().getPI());
System.out.println("PI in ThatUniverse is: "
+ new ThatUniverse().getPI());
System.out.println("PI in TheOtherUniverse is: "
+ new TheOtherUniverse().getPI());
}
}
abstract class BasePI
{
abstract public double PI();
}
class RomanPI extends BasePI
{
public double PI() { return 5.234; }
}
class GreekPI extends BasePI
{
public double PI() { return 4.78; }
}
class CarthagenianPI extends BasePI
{
public double PI() { return 3.14159; }
}
abstract class Universe
{
public double getPI() { return pi.PI(); }
protected Universe(BasePI pi) { this.pi = pi; }
private BasePI pi;
}
class ThisUniverse extends Universe
{
public ThisUniverse() { super(new GreekPI()); }
}
class ThatUniverse extends Universe
{
public ThatUniverse() { super(new RomanPI()); }
}
class TheOtherUniverse extends Universe
{
public TheOtherUniverse() { super(new CarthagenianPI()); }
}
// ------------------------------------------------------