Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > overwriting fields from abstract class

Reply
Thread Tools

overwriting fields from abstract class

 
 
Peter Grison
Guest
Posts: n/a
 
      04-27-2004
I have an abstract class MyTable with a constant field like

public static final String[] THEFIELDS =
{"id","key", "keyvalue"};

I use this to implement some methods in MyTable
public int numberOfFields() {
return THEFIELDS.length;
}

I created an implementation of the abstract class where I have
overwritten THEFIELDS with a different numbers of constants.

public class TheUser
extends MyTable
{

public static final String[] THEFIELDS =
{"id","username", "password" , "usergroup", "age"};

The result of numberOfFields in an instance the_user of the TheUser
implementation returns however the numberOfFields value from the
ancestor class where I expected it to return the child's number of
fields. What am I doing wrong?


peter

 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      04-27-2004

"Peter Grison" <(E-Mail Removed)> wrote in message
news:c6l50s$58b$(E-Mail Removed)1.nb.home.nl...
> I have an abstract class MyTable with a constant field like
>
> public static final String[] THEFIELDS =
> {"id","key", "keyvalue"};
>
> I use this to implement some methods in MyTable
> public int numberOfFields() {
> return THEFIELDS.length;
> }
>
> I created an implementation of the abstract class where I have
> overwritten THEFIELDS with a different numbers of constants.
>
> public class TheUser
> extends MyTable
> {
>
> public static final String[] THEFIELDS =
> {"id","username", "password" , "usergroup", "age"};
>
> The result of numberOfFields in an instance the_user of the TheUser
> implementation returns however the numberOfFields value from the
> ancestor class where I expected it to return the child's number of
> fields. What am I doing wrong?


There can only be one static member of the same name within an inheritence
structure, that is the meaning of static, you are trying to have two.

Your superclass is calling the length method and it will refer to its copy
of THEFIELDS

If you do:
public class TheUser extends MyTable {

{ THEFIELDS = {"id","username", "password" , "usergroup", "age"}; }

}

in the subclass.

in your sub class (which you can't because it is final) then it will only
succeed in replacing the super classes THEFIELDS for all instances of all
classes, since there can only be one static member of the same name. So you
will probably be setting yourself up for trouble.
The bottom line is don't try and use static members to participate in
polymorphism.

--
Mike W


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      04-27-2004
Peter Grison wrote:
> I have an abstract class MyTable with a constant field like
>
> public static final String[] THEFIELDS =
> {"id","key", "keyvalue"};
>
> I use this to implement some methods in MyTable
> public int numberOfFields() {
> return THEFIELDS.length;
> }


Okay...

> I created an implementation of the abstract class where I have
> overwritten THEFIELDS with a different numbers of constants.
>
> public class TheUser
> extends MyTable
> {
>
> public static final String[] THEFIELDS =
> {"id","username", "password" , "usergroup", "age"};


Two things:

First (and this is just a technical point), there is no such concept in
Java as "overwriting" something. Perhaps you mean that are
"overriding" the field.

Second, if you do mean that you are overriding the field, then you're
wrong. It is, in fact, impossible to override a field. Only methods
may be overridden. The simple solution is to replace your field with an
accessor method:

public abstract class MyTable
{
public String[] getFields()
{
return new String[] { "id", "key", "keyvalue" };
}

public final int numberOfFields()
{
return getFields().length;
}

...
}

and then,

public final class TheUser extends MyTable
{
public String[] getFields()
{
return new String[] {
"id", "username", "password", "usergroup", "age"
};
}

...
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-27-2004
On Tue, 27 Apr 2004 08:04:10 -0600, Chris Smith <(E-Mail Removed)>
wrote or quoted :

>Second, if you do mean that you are overriding the field, then you're
>wrong. It is, in fact, impossible to override a field. Only methods
>may be overridden.


fields can be shadowed, methods overridden.

See http://mindprod.com/jgloss/gotchas.html
and search for the word "recipe". There is big example that explains
the difference.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
Abstract Methods & Abstract Class Iyer, Prasad C Python 0 10-20-2005 06:35 AM
About abstract class and abstract method Sameer Java 4 08-31-2005 12:59 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Abstract class with no abstract functions Uzytkownik C++ 3 04-03-2005 05:45 PM



Advertisments