Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:02:36+00:00 2026-05-25T14:02:36+00:00

I’m having a problem in relation to using super and overriding. Basically, class B

  • 0

I’m having a problem in relation to using super and overriding. Basically, class B which extends A has a setter for the current state of the class. Inside the setter, depending on the value of the current state, a different event can be executed.

In the setter for B the first thing that happens is that it called super so that the setter for A can go launch general events. Then the control returns to the setter of B where I can execute specific events if needed.

The problem comes when A executes events that call the setter and so it can go multiple depths before returning back to to B.

The following code illustrates what I’m talking about (it’s groovy, but that’s irrelevant):

class A
{
    public int num = 0;
    public void setFoo( int i ) 
    { 
        println "A: $i"; 
        num = i + 1;

        // what's actually happening, is that in the setter, depending
        // on the value, an event can be executed, which will in turn
        // call setFoo() on the class. this is just the equivalet
        if( i < 3 )
            this.setFoo( num );
    }
}
class B extends A
{
    public void setFoo( int i ) 
    {
        println "B: $i - num $num";
        super.setFoo( i );
        println "After super for $i - num: $num";
    }
}

def B = new B();
B.foo = 0;

This results in an output of:

B: 0 - num 0
A: 0
B: 1 - num 1
A: 1
B: 2 - num 2
A: 2
B: 3 - num 3
A: 3
After super for 3 - num: 4
After super for 2 - num: 4
After super for 1 - num: 4
After super for 0 - num: 4

When I come back to B after the call to super (“After super for…”) the value of num is always the same, meaning that it screws with what I’m trying to do in B (i.e. launch specific events).

Some points on the architecture to begin with:

  • “Why not use i instead of num in the setter for B“? – This is just the easiest example to show the problem – what’s actually happening in my code is different, just the same problem. In my case, I have access to num, not i. Even if I rewrote part of it to pass i, the state of the class will have moved on (due to the base class)
  • It’s a server environment, so I don’t have access to a frame loop or something similar. It’s event based.
  • It should be possible to async execute the event, or set the event up to schedule later, but that requires a lot of advance knowledge of where and when the event is going to be used, which breaks the whole point of events in the first place

What I’m looking for is a way to launch events based on the state of the class, but have it happen after the return from super (while still working for the base class) if that makes any sense.

Ideas?

EDIT

To give a better idea of the code I’m using (based on Don’s suggestion to use a callback), here is a simplified version of what I have. (If you want to run it, you can just copy it into http://groovyconsole.appspot.com/):

// A is our base class
​class A{
    public int currentState= 0;
    public void setCurrentState( int i ) 
    { 
        this.currentState = i;
        this._onStateChanged();
    }

    protected void _onStateChanged()
    {
        println "The state in A is $currentState";

        // depending on the state launch some events.
        // these can changed the current state of
        // B
        if( this.currentState == 0 )
        {
            def event = new MyEvent( this );
            event.execute();
        }
    }
}

// B is a more specific version of A
class B extends A
{
    protected void _onStateChanged()
    {
        println "The state in B is $currentState";
        super._onStateChanged();
        println "The state in B afterwards is $currentState";

        // launch specific events based on the current state
        if( this.currentState == 0 )
           println "Launch a specific event!";
    }
}

// simple event class that can change the status of B
class MyEvent
{
    private B b = null;
    public MyEvent( B b )
    {
        this.b = b;
    }
    public void execute()
    {
        // do some stuff
        b.currentState++;
    }
}

// program start
def b = new B();
b.currentState = 0;​

B has to call super as there are some states where I want a basic plus a specific event. Basic events are normally used to set the program state, while specific ones are there to react.

In this example, my output is:

The state in B is 0
The state in A is 0
The state in B is 1
The state in A is 1
The state in B afterwards is 1
The state in B afterwards is 1

i.e. B never gets to react to the state being 0

Edit

If I change the super() call in B to the end of _onStateChanged() rather than the start, this will give the chance for it to react to the state before it gets changed. Is this a simple solution to this problem, or just wrong?

Edit
So I came up with this (again, you can copy it into the groovy console appspot site):

// A is our base class
class A{
    public int currentState = 0;
    public int nextState = 0;
    public boolean canChange = true;
    public void setCurrentState( int i ) 
    { 
        if( this.canChange )
        {
            this.currentState = i;
            this._onStateChanged();
        }
        else
            this.nextState = i;
    }

    protected void _onStateChanged()
    {
        println "The state in A is $currentState";

        // depending on the state launch some events.
        // these can changed the current state of
        // B
        if( this.currentState == 0 )
        {
            def event = new MyEvent( this );
            event.execute();
        }
    }
}

// B is a more specific version of A
class B extends A
{
    protected void _onStateChanged()
    {
        this.canChange = false;
        println "The state in B is $currentState";
        super._onStateChanged();
        println "The state in B afterwards is $currentState";

        // launch specific events based on the current state
        if( this.currentState == 0 )
           println "Launch a specific event!";

        this.canChange = true;
        if( this.nextState != 0 )
        {
            int state = this.nextState;
            this.nextState = 0;
            this.currentState = state;
        }
    }
}

// simple event class that can change the status of B
class MyEvent
{
    private B b = null;
    public MyEvent( B b )
    {
        this.b = b;
    }
    public void execute()
    {
        // do some stuff
        b.currentState++;
    }
}

// program start
def b = new B();
b.currentState = 0;​

It gives me the desired output:

The state in B is 0
The state in A is 0
The state in B afterwards is 0
Launch a specific event!
The state in B is 1
The state in A is 1
The state in B afterwards is 1

but is kind of ugly. Better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:02:37+00:00Added an answer on May 25, 2026 at 2:02 pm

    Ok, so I have two solutions to this. This first one is the last code sample provided. It adds another parameters to check if we can change, and if so, does, otherwise it waits:

    // A is our base class
    class A{
        public int currentState = 0;
        public int nextState = 0;
        public boolean canChange = true;
        public void setCurrentState( int i ) 
        { 
            if( this.canChange )
            {
                this.currentState = i;
                this._onStateChanged();
            }
            else
                this.nextState = i;
        }
    
        protected void _onStateChanged()
        {
            println "The state in A is $currentState";
    
            // depending on the state launch some events.
            // these can changed the current state of
            // B
            if( this.currentState == 0 )
            {
                def event = new MyEvent( this );
                event.execute();
            }
        }
    }
    
    // B is a more specific version of A
    class B extends A
    {
        protected void _onStateChanged()
        {
            this.canChange = false;
            println "The state in B is $currentState";
            super._onStateChanged();
            println "The state in B afterwards is $currentState";
    
            // launch specific events based on the current state
            if( this.currentState == 0 )
               println "Launch a specific event!";
    
            this.canChange = true;
            if( this.nextState != 0 )
            {
                int state = this.nextState;
                this.nextState = 0;
                this.currentState = state;
            }
        }
    }
    
    // simple event class that can change the status of B
    class MyEvent
    {
        private B b = null;
        public MyEvent( B b )
        {
            this.b = b;
        }
        public void execute()
        {
            // do some stuff
            b.currentState++;
        }
    }
    
    // program start
    def b = new B();
    b.currentState = 0;​
    

    The second solution takes a more listener like approach. Both the base and the extending class register functions to call when the state changes:

    // A is our base class
    class A{
        public int currentState = 0;
        public def listeners = [];
        public void setCurrentState( int i ) 
        { 
            // call each of our listeners with the current state
            this.currentState = i;
            listeners.each { it( i ); }
        }
    
        public A()
        {
            this.addListener( this.&_onStateChanged );
        }
    
        public void addListener( def callback )
        {
            this.listeners.add( 0, callback );
        }
    
        protected void _onStateChanged( int state )
        {
            println "The state in A is $state";
    
            // depending on the state launch some events.
            // these can changed the current state of
            // B
            if( state == 0 || state == 1 )
            {
                def event = new MyEvent( this );
                event.execute();
            }
        }
    }
    
    // B is a more specific version of A
    class B extends A
    {
        public B()
        {
            super();
            this.addListener( this.&_onBStateChanged );
        }
    
        protected void _onBStateChanged( int state )
        {
            println "The state in B is $state";
    
            // launch specific events based on the current state
            if( state == 0 )
                println "Launch a specific event!";
        }
    }
    
    // simple event class that can change the status of B
    class MyEvent
    {
        private B b = null;
        public MyEvent( B b )
        {
            this.b = b;
        }
        public void execute()
        {
            // do some stuff
            b.currentState++;
        }
    }
    
    // program start
    def b = new B();
    b.currentState = 0;
    

    Both give me the output I’m looking for, though the second one is slightly broken, as it breaks the normal convention of adding listeners. Listeners are added to the start of the list, rather than the end. That’ll give me an output like:

    The state in B is 0
    Launch a specific event!
    The state in A is 0
    The state in B is 1
    The state in A is 1
    The state in B is 2
    The state in A is 2
    

    So B gets called first, but at least they’re in a good order. If I just push listeners to the list, I get:

    The state in A is 0
    The state in A is 1
    The state in A is 2
    The state in B is 2
    The state in B is 1
    The state in B is 0
    Launch a specific event!
    

    So B will get to react to the state being 0, but the order is reversed and by that stage, the state has changed to something else. It also breaks a bit in that it requires knowledge that B will never launch an event that will change the state, as otherwise we still have the same problem.

    Out of the two, I think the first one is the best (assuming a non-rewrite of the architecture/problem). It’s a bit more complicated, but doesn’t require prior knowledge anywhere and the events get called in the right order.

    Unless someone can suggest a better architecture for the problem, I’ll go with that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.