overwriting fields from abstract class

Discussion in 'Java' started by Peter Grison, Apr 27, 2004.

  1. Peter Grison

    Peter Grison Guest

    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
    Peter Grison, Apr 27, 2004
    #1
    1. Advertising

  2. Peter Grison

    VisionSet Guest

    "Peter Grison" <> wrote in message
    news:c6l50s$58b$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
    VisionSet, Apr 27, 2004
    #2
    1. Advertising

  3. Peter Grison

    Chris Smith Guest

    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
    Chris Smith, Apr 27, 2004
    #3
  4. Peter Grison

    Roedy Green Guest

    On Tue, 27 Apr 2004 08:04:10 -0600, Chris Smith <>
    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.
    Roedy Green, Apr 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    438
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    601
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    592
    Uzytkownik
    Apr 3, 2005
  4. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    532
    Iyer, Prasad C
    Oct 20, 2005
  5. Replies:
    4
    Views:
    813
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page