recast a superclass attribute

Discussion in 'Java' started by VisionSet, Dec 3, 2003.

  1. VisionSet

    VisionSet Guest

    class MySuper {

    Date date;
    }

    class MySub extends MySuper {

    MyDate date = (MyDate)super.date;
    }

    Is this sort of thing okay?
    I feel I need to do it, but not seen it done before.

    I wanted to make my superclass as general as possible so used the Date type,
    but subclasses invariably instantiate date themselves as a subclass of Date,
    and then need to use it as such.

    --
    Mike W
     
    VisionSet, Dec 3, 2003
    #1
    1. Advertising

  2. "VisionSet" <> wrote in message
    news:9Opzb.1001$...
    >
    > class MySuper {
    >
    > Date date;
    > }
    >
    > class MySub extends MySuper {
    >
    > MyDate date = (MyDate)super.date;
    > }
    >
    > Is this sort of thing okay?



    Only if Date is derived from MyDate. i.e., MyDate is the superclass of
    Date. date needs to contain a reference to an object of the MyDate class.

    > I feel I need to do it, but not seen it done before.
    >
    > I wanted to make my superclass as general as possible so used the Date

    type,
    > but subclasses invariably instantiate date themselves as a subclass of

    Date,
    > and then need to use it as such.
    >


    Why bother having a date in the superclass at all?


    --
    Shripathi Kamath
     
    Shripathi Kamath, Dec 3, 2003
    #2
    1. Advertising

  3. VisionSet

    VisionSet Guest

    "Shripathi Kamath" <s h r i p a t h i k a m a t h @ h o t m a i l . c o m>
    wrote in message news:...
    >
    > "VisionSet" <> wrote in message
    > news:9Opzb.1001$...
    > >
    > > class MySuper {
    > >
    > > Date date;


    MySuper(Date date) { // I throw in a constructor or two for you.
    this.date = date;
    }


    > > }
    > >
    > > class MySub extends MySuper {
    > >
    > > MyDate date = (MyDate)super.date;


    MySub(MyDate date) {
    super(date);
    }

    > > }
    > >
    > > Is this sort of thing okay?

    >
    >
    > Only if Date is derived from MyDate. i.e., MyDate is the superclass of
    > Date. date needs to contain a reference to an object of the MyDate class.


    Surely the wrong way round!
    I can legally cast my 'Date date' to MyDate in the subclass, because the
    subclass controls the type of this object.

    MyDate is a subclass of java.util.Date


    >
    > > I feel I need to do it, but not seen it done before.
    > >
    > > I wanted to make my superclass as general as possible so used the Date

    > type,
    > > but subclasses invariably instantiate date themselves as a subclass of

    > Date,
    > > and then need to use it as such.
    > >

    >
    > Why bother having a date in the superclass at all?


    erm, because the superclass has methods that operate on the Date object and
    do not care if it is actually a subclass of Date.

    --
    Mike W
     
    VisionSet, Dec 3, 2003
    #3
  4. Huh? I don't get your question.

    What is MyDate? Is it a derived class from Date?
     
    Eki Y. Baskoro, Dec 18, 2003
    #4
  5. VisionSet wrote:
    > class MySuper {
    >
    > Date date;
    > }
    >
    > class MySub extends MySuper {
    >
    > MyDate date = (MyDate)super.date;
    > }
    >
    > Is this sort of thing okay?
    > I feel I need to do it, but not seen it done before.


    It is OK, if you pay attention to the following: both classes have their
    own field date. It means that if you modified the field in some way,
    they will have different values. Which one you access depends on the
    static type at the point you access the field. In particular, a method
    defined in MySuper will access the MySuper.date field, even if 'this' is
    an instance of MySub.

    For this reason, I would advise you only to do this with _final_ fields.
     
    Daniel Bonniot, Dec 19, 2003
    #5
    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. James
    Replies:
    6
    Views:
    318
    Christoph Haas
    May 9, 2006
  2. David
    Replies:
    1
    Views:
    414
    mlimber
    Jan 17, 2006
  3. Evan Klitzke
    Replies:
    0
    Views:
    368
    Evan Klitzke
    Aug 2, 2007
  4. Housing
    Replies:
    5
    Views:
    596
    Old Wolf
    Sep 12, 2007
  5. bart van deenen
    Replies:
    6
    Views:
    762
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page