473,657 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(S tring 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.prin tln(bob);
System.out.prin tln(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.s etPI(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 3767

"Ovid" <pu************ **********@yaho o.com> wrote in message
news:14******** *************** ***@posting.goo gle.com...
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(S tring 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.prin tln(bob);
System.out.prin tln(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.s etPI(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.prin tln("PI in ThisUniverse is: "
+ new ThisUniverse(). getPI());

System.out.prin tln("PI in ThatUniverse is: "
+ new ThatUniverse(). getPI());

System.out.prin tln("PI in TheOtherUnivers e is: "
+ new TheOtherUnivers e().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 TheOtherUnivers e extends Universe
{
public TheOtherUnivers e() { 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(S tring 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.prin tln(bob);
System.out.prin tln(ovid);
}
}
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6731
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) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
15
24001
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 overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a real world example of using virtual functions would be very much appreciated.
4
1927
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 the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
3
1546
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
4727
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
3
8670
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 { public:
4
1782
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 into the browser equivalent like &lt; &gt; etc.( for example by using the cgi module) Case 2: Whenever I wanted to present the output to some client other than a browser, I wanted to present the data as it is stored in the database.
4
2346
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
105177
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
8397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.