473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overriding class data

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
2 3745

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo {...
4
by: Raja Raman Sundararajan | last post by:
Hello guys, I have data stored in the database which has special characters like <, etc. Case 1: Whenever I wanted to present the output to a browser I need to escape these special characters...
4
dmjpro
by: dmjpro | last post by:
Look at my code carefully..... class Super { protected int a; } class Derived extends Super { protected int a; //Here Data Overriding or Data Hiding
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.