Inheritence and kind of doing it backwords.

Discussion in 'Java' started by Mitch, Feb 25, 2006.

  1. Mitch

    Mitch Guest

    Hi.

    My problem is quite specific, but doing a search on inheritence was
    just too much to sift through.

    This post IS very long, and I am sorry, but I've gone for clarity
    rather than brevity

    I'll try to explain my specific problem in detail, though I expect the
    answer is quite simple and common, for that I apologise.



    I am writing a rail simulation piece, with each segment of rail an
    object. I am then extending the railSection class to have more
    specialised railSection objects, such as Signal and Point (to Americans
    i think they are called intersections or junctions)...

    Now if i have a 'train' running along the track (which is currently an
    array or map (prob array) of railSections, how will i test to see if
    say a signal is red?

    I have a line which currently reads:

    ///////

    while(this.currentRailSection.whatIsSignal() == signalState.RED)
    this.currentRailSection.redSignal.await();

    ////////

    Now obviously the 'currentRailSection' could be an extended Signal
    railSection or just a standard railSection, and as the compiler see's
    it as a standard railSection it flags a message.

    Now if the Signal class inherited the 'signalState' from the
    railSection i imagine all would be fine, however that is not how i
    would like it to be. So i guess my question is how do I test for
    something in an extended class when i have to refer to it as the base
    class...?


    I think an abstraction:

    ////////
    class a
    {
    blah
    }

    class b extends a
    {
    blah...

    private variable whatIsMyVariable() { return _variable; }

    private variable _variable;
    }

    -=-=-=-=-=-=-

    Now i want to do

    while(a<100; a++)
    IF(a.whatIsMyVariable == 123)
    do something;

    Knowing that it will return false whenever the class a ISN'T actually
    b..

    ////////


    So i would have an array which would be my route which would go
    something like

    railSection
    railSection
    railSection
    railSection
    Signal extends railSection
    railSection
    railSection
    railSection

    And prior to carrying onto the next railSection i want to test to see
    if my signal is red.


    Hope I have made myself clear and thank you to all who take the time to
    read it, I know it's a beast.

    Kind Regards,

    Mitch.
     
    Mitch, Feb 25, 2006
    #1
    1. Advertising

  2. Mitch

    Mitch Guest

    I'd like to point out that with the line:

    ///////

    while(this.currentRailSection.whatIsSignal() == signalState.RED)
    this.currentRailSection.redSignal.await();

    ////////

    It isn't what is in the while statement that is the problem, that was
    easily overcome by adding a whatIsSignal method in the railSection
    class which returned signalState.NONE

    It the is the 'condition' redSignal that is flagging errors.

    this.currentRailSection.redSignal.await();

    Although this line only executes if the while() is valid it still
    errors in the compiler and as such I need to fix it.
     
    Mitch, Feb 25, 2006
    #2
    1. Advertising

  3. Mitch wrote:
    > Hi.
    >
    > My problem is quite specific, but doing a search on inheritence was
    > just too much to sift through.
    >
    > This post IS very long, and I am sorry, but I've gone for clarity
    > rather than brevity
    >
    > I'll try to explain my specific problem in detail, though I expect the
    > answer is quite simple and common, for that I apologise.
    >
    >
    >
    > I am writing a rail simulation piece, with each segment of rail an
    > object. I am then extending the railSection class to have more
    > specialised railSection objects, such as Signal and Point (to Americans
    > i think they are called intersections or junctions)...



    snipped..


    one quick observation...... The Train (driver) needs to know what the
    signal is... not the tracks.

    The signals are not special track segments...

    andrew
     
    Andrew McDonagh, Feb 25, 2006
    #3
  4. Mitch

    Paul Hamaker Guest

    You could do something like this :
    if ( baseref instanceof Signal ) {
    sigref = (Signal) baseref;
    if ( sigref.getSigState() ) etc...

    --------------------
    Paul Hamaker, SEMM, teaching ICT since 1987
    http://javalessons.com
     
    Paul Hamaker, Feb 25, 2006
    #4
  5. Mitch

    Mitch Guest

    I agree in some respects. For instance the code you see above is found
    inside my Train class. I.E. it is the train that is checking for the
    red signal and then waiting accordingly.

    I don't disagree that the signals are not special track segments
    either, not in reality, but there are two major points to be considered
    from my simulation's point of view. The first is that I need to
    specify routes for the trains. Therefore i need for the train to have
    an idea of which part of the track it is on and which part of track is
    next. So in the example above where i say i need to specify the route
    as
    railSection
    railSection
    railSection
    signal
    railSection
    .....

    It would be considerably easier for me, my marker, and for modularity
    if signal was infact extended from the railSection class. It makes
    route specification, train location etc a lot easier.

    My second point is that the railSections for the most part also control
    the signals. IE if there is a train on the next section of track you
    want the preceeding signal to be red (This is specified, and is how it
    is done R/L in Britain and most if not all over the world).

    For the most part however it boils down to route planning. I need to
    specify an array of railSections for the train to travel along, and
    adding sections that aren't rail will cause me troubles, hopefully less
    than the troubles this condition is causing me anyway.

    Thanks for your observations though :)
     
    Mitch, Feb 25, 2006
    #5
  6. Mitch

    Mitch Guest

    "instanceof" is an operator I haven't heard of before and could well be
    what I'm looking for, thanks!
     
    Mitch, Feb 25, 2006
    #6
  7. Mitch

    Mitch Guest

    Perfect!

    I have turned what i had:

    ////////

    while(this.currentRailSection.whatIsSignal() == signalState.RED)
    this.currentRailSection.redSignal.await();

    ////////

    into this

    ////////

    if(this.whatIsCurrentRailSection() instanceof Signal){

    signalReference = (Signal)this.whatIsCurrentRailSection();

    while( signalReference.whatIsSignal() == signalState.RED){
    signalReference.redSignal.await();

    }

    }

    ////////

    And all seems to work perfectly!

    Thank you very much, was exactly what I was looking for!
     
    Mitch, Feb 25, 2006
    #7
  8. Mitch

    Chris Uppal Guest

    Mitch wrote:

    > "instanceof" is an operator I haven't heard of before and could well be
    > what I'm looking for, thanks!


    It isn't. If you control all aspects of a design, and find yourself using
    instanceof (except in an equals(Object o) method), then it's almost certainly a
    symptom of poor design.

    -- chris
     
    Chris Uppal, Feb 25, 2006
    #8
  9. Mitch

    Chris Uppal Guest

    Mitch wrote:

    > My second point is that the railSections for the most part also control
    > the signals. IE if there is a train on the next section of track you
    > want the preceeding signal to be red (This is specified, and is how it
    > is done R/L in Britain and most if not all over the world).


    How about this:

    Say you have a base class called TrackElement. You have subclasses called
    RailSegment (representing a stretch of actual rail), Signal, end whatever.

    You might have a collection of all the TrackElements, but the Train does not
    access that collection directly. Instead each RailSegment knows which
    Signal(s) it controls and which RailSegment(s) it connects to. The Train knows
    which RailSegment it is on, and asks that for the Signal controlling it's
    movement in whichever direction it wants to go. If that Signal is green, then
    the Train asks the RailSegment for the next RailSegment and moves on to that.

    There are other ways of setting it up -- for instance the Train could ask the
    Signal for RailSegment to which it controls access, rather than asking the
    RailSegment it is currently on. I think that's rather neater, on the whole.

    Thus the Train never traverses any Signals, only RailSegments; and never
    checks the status of a RailSegment, only of Signals.

    -- chris
     
    Chris Uppal, Feb 25, 2006
    #9
  10. Mitch

    Mitch Guest

    Ok I have yet to see a problem but I have also yet to test it.
    The code supplied below seems to overcome my problem but it may cause
    me another.

    ////////

    if(this.whatIsCurrentRailSection() instanceof Signal){

    signalReference = (Signal)this.whatIsCurrentRailSection();

    while( signalReference.whatIsSignal() == signalState.RED){
    signalReference.redSignal.await();

    }

    }

    ////////

    This boils down to how Java works internally.

    At the bottom of that class I have private Signal
    signalReference; and then in the code above I say

    when reference = RED
    await();

    Await is a method relating to conditions in threads. If the reference
    is RED then I await another thread to .signalAll() (which happens when
    another thread changes a signal from red to something else) and this
    then forces the when reference = RED check to check again to see if in
    fact it was our red signal that was changed.

    Now my question is Once i have created signalReference, will the
    condition always be the same. (I.E. Have i copied the values of the
    Signal object which means they will never change) or will they change
    as well?
     
    Mitch, Feb 25, 2006
    #10
  11. Mitch

    Mitch Guest

    I definately agree that your way is better design, however I simply
    can't do it that way. This is a programming project, but fortunately
    or unfortunately I'm not yet sure, it isn't going to be marked by any
    programmers, and only be seen in a report and a 15 minute presentation.

    It boils down to lack of time. I have two weeks in which to finish
    something I can display to my lecturers. Scalability is a term they
    will know and I can play on. If I have a route which is initiated as
    an array (maybe read in from a text file) then it will be easy to show
    them the route, and easy for me to then tel them how easy it would be
    for them to change the route (say for rail maintenence). Doing it your
    way, although better, would be more difficult to explain, and I wouldn'
    be able to show them how to add / modify a route in the time of the
    presentation.

    I will at least mention your way if you permit in my report as a better
    way of doing things? ( I will have to reference this topic anyay)

    Cheers!

    Mitch
     
    Mitch, Feb 25, 2006
    #11
  12. Mitch

    Chris Uppal Guest

    Mitch wrote:

    > It boils down to lack of time. I have two weeks in which to finish
    > something I can display to my lecturers.


    Unfortunate...


    > I will at least mention your way if you permit in my report as a better
    > way of doing things? ( I will have to reference this topic anyay)


    Sure. Be my guest.

    (Really, there was no need to ask -- everything on Usenet is public anyway --
    but I appreciate your courtesy.)

    -- chris
     
    Chris Uppal, Feb 25, 2006
    #12
  13. Mitch wrote:
    ....
    > Now obviously the 'currentRailSection' could be an extended Signal
    > railSection or just a standard railSection, and as the compiler see's
    > it as a standard railSection it flags a message.

    ....

    A signal is not a type of rail section. A signal is something some, but
    not all, rail sections have.

    I would move out all code related to signal management to a separate
    Signal class, not derived from RailSection. It is possible signalState
    should disappear into the reformed Signal:

    while(currentRailSection.hasSignal() &&
    currentRailSection.getSignal().whatIsSignal() == Signal.RED)


    If you make Signal a subclass, how would you handle switches that have
    signals? It's trivial if you treat having a signal as an optional
    attribute of any rail segment.


    Patricia
     
    Patricia Shanahan, Feb 25, 2006
    #13
  14. Mitch

    Roedy Green Guest

    On 25 Feb 2006 05:09:28 -0800, "Mitch" <>
    wrote, quoted or indirectly quoted someone who said :

    >"instanceof" is an operator I haven't heard of before and could well be
    >what I'm looking for, thanks!


    see http://mindprod.com/jgloss/instanceof.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 25, 2006
    #14
  15. Chris Uppal wrote:
    > Mitch wrote:
    >
    >
    >>"instanceof" is an operator I haven't heard of before and could well be
    >>what I'm looking for, thanks!

    >
    >
    > It isn't. If you control all aspects of a design, and find yourself using
    > instanceof (except in an equals(Object o) method), then it's almost certainly a
    > symptom of poor design.
    >
    > -- chris
    >
    >


    very strongly seconded!
     
    Andrew McDonagh, Feb 26, 2006
    #15
  16. Mitch

    Chris Uppal Guest

    Mitch wrote:

    > when reference = RED
    > await();


    Unfortunately that isn't Java so I don't know exactly what the pseudo-code is
    supposed to mean.


    > Now my question is Once i have created signalReference, will the
    > condition always be the same. (I.E. Have i copied the values of the
    > Signal object which means they will never change) or will they change
    > as well?


    If the signalReference variable holds a reference to an object (its an odd name
    for a variable, btw), and if you don't assign a ref to a different object to
    it, then it will (this is kinda obvious ;-) still refer to the same object. If
    the state of that object changes over time, then whenever you use that variable
    it will still contain a reference to the same object, and so you will see that
    object's current state each time.

    E.g:

    Signal signal = someSignalOrOther();
    signal.isRed(); // returns true

    ... time passes during which other code executes on
    ... another thread and changes the state of the signal

    signal.isRed(); // returns false

    Does that answer your question, or have I misunderstood you ?

    -- chris
     
    Chris Uppal, Feb 26, 2006
    #16
  17. Mitch

    Mitch Guest

    Chris, you understood with that last post, and yeah it did answer so
    thank you.

    Also thank you Roedy, I had found that after the recommendation.

    And Patricia thank you for your comments.
    As I said to Chris before there is the unfortunate problem of this
    being marked by non-programmers and so I am trying to use as many key
    topics as I can, one of which is inheritence. That is why I have done
    it that way. My original code had one RailSection class with
    attributes such as hasSignal and hasPoint. I changed it with good
    reason, though it seems to have lost me somewhere. I am however
    strongly considering re-writing the class at the moment. I am having
    troubles with Conditions which are new to me and I think simplifying to
    overal structure could help.

    Thank you all for your help and I will report back here as to what I
    do,

    Kind Regards,

    Mitch.
     
    Mitch, Feb 26, 2006
    #17
  18. Mitch

    Mitch Guest

    Patricia.

    Having started viewing these groups a lot lately i decided to go and
    get myself a newsreader and stop using google groups. Your post must
    have been in this transition because I have only just noticed it. (I
    am searching for those relating to my project to reference, in case you
    were wondering.)

    That is indeed a better way of doing it, I only wish I had seen it
    sooner. The instanceof method is working for me reliably, however, and
    I haven't the time to change it now but thank you anyway.

    Kind Regards,

    Mitch.

    Regarding the switches (and stations) that have signals, they are
    placed immeadiately before / after / where they should be in the system
    and know of local rail sections so I have found a way around it.

    eg

    RailSection
    RailSection
    Signal
    Point(switch)
    / \
    RailSection RailSection

    Thanks again!

    Mitch.
     
    Mitch, Mar 14, 2006
    #18
    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. Eric Biggs

    vs designer and code-behind inheritence

    Eric Biggs, Apr 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    330
    Eric Biggs
    Apr 14, 2004
  2. krzyber
    Replies:
    1
    Views:
    318
    Josh Martin
    Nov 20, 2003
  3. Ian McBride
    Replies:
    2
    Views:
    442
    Alf P. Steinbach
    Jul 19, 2003
  4. exits funnel
    Replies:
    3
    Views:
    380
    Ron Natalie
    Dec 21, 2003
  5. gorda
    Replies:
    16
    Views:
    780
    Karl Heinz Buchegger
    Jul 29, 2004
Loading...

Share This Page