By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,553 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,553 IT Pros & Developers. It's quick & easy.

Overriding class data

P: n/a
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?

Cheers,
Ovid
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"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()); }
}

// ------------------------------------------------------
Jul 17 '05 #2

P: n/a
Here is something better using Java package and access control:

package Universe;

public class Universe
{
private String name;
private double PI;

public Universe(String name)
{
this(name, 3.1415);
}

protected Universe(String name, double PI)
{
this.name = name;
this.PI = PI;
}

public String toString()
{
return "Universe: " + name + " PI: " + PI;
}

}//eof

package Universe;

public class UniverseRoman extends Universe
{
public UniverseRoman(String name)
{
super(name, 3);
}

}

package TestUniverse;

import Universe.*;
class TestUniverse
{
public static void main(String args[])
{
Universe bob = new Universe("Bob");
//Universe error = new Universe("error", 5);
UniverseRoman ovid = new UniverseRoman("Ovid");
System.out.println(bob);
System.out.println(ovid);
}
}
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.