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

  • SEARCH
  • Home
  • 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 196081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:44:13+00:00 2026-05-11T16:44:13+00:00

This question is meant to apply to interfaces in general, but I’ll use AS3/Flex

  • 0

This question is meant to apply to interfaces in general, but I’ll use AS3/Flex for my language. It should be [mostly] obvious how to apply it in different languages.

If I create a base class, and it extends an interface, there is an explicit contract defined: for every method in the interface, the base class must implement said method.

This is easy enough. But I don’t understand why you have the capacity to cast an interfaced instance back to its original base class. Of course, I’ve had to do this a few times (the example below is very close to the situation I’m struggling with), but that doesn’t mean I understand it :^)

Here’s a sample interface:

public interface IFooable extends IUIComponent {
    function runFoo():void;
}

Let’s say I create a base class, which extends VBox and implements the interface:

public class Foo extends VBox implements IFooable {
    public Foo() {
        super();
        //stuff here to create Foo..blah blah
    }
    public function runFoo():void {
        // do something to run foo
    }
}

Now, the reason I used the interface, is because I want to guarantee “runFoo” is always implemented. It is a common piece of functionality all of my classes should have, regardless of how they implement it. Thus, my parent class (an Application) will instantiate Foo via its interface:

public function init():void {
    var foo:IFooable = new Foo();
    foo.percentHeight = 100; //works because of IUIComponent
}

But, if I want to add Foo to the Application container, I now have to cast it back to the base class (or to a different base class):

public function init():void {
    var foo:IFooable = new Foo();
    foo.percentHeight = 100;

    addChild(foo as DisplayObject); //_have_ to cast, because addChild takes a 'DisplayObject' class type

    //could also do this:
    //addChild(foo as VBox);
}

Wasn’t the original intention to hide the implementation of Foo? There is still an assumption that Foo is a DisplayObject. Unfortunately, being able to add the custom object to a container seems impossible without casting.

Am I missing something entirely? Is this really just a phenomenon in Flex/AS3? If you have a container in the base API of a language, and it only allows you to add children of a certain class type, how do you then abstract out implementation?

For the record, this question appears to ask if this sort of operation is possible, but it doesn’t really address why it might be bad design (and how to fix it).


2nd Thought:

Abstract Classes:

As Matthew pointed out, abstract classes helps solve some of this: I could create a base abstract class which inherits from the DisplayObject (or, in my case, the VBox, since it is a child of DisplayObject), and have the base class implement the interface. Thus, any class which extends the abstract class would then be required to implement the methods therein.

Great idea — but AS3 doesn’t have abstract classes (to my knowledge, anyway).

So, I could create a base class which implements interface and extends the VBox, and inherit from it, and I could insert code in those methods which need to be extended; such code would throw an error if the base class is the executor. Unfortunately, this is run-time checking as opposed to compile-time enforcement.

It’s still a solution, though.


Context:

Some context might help:

I have an application which can have any number of sub-containers. Each of these sub-containers will have their own respective configuration options, parameters, etc. The application itself, however, has a global ApplicationControlBar which will contain the entry-point Menu for accessing these configuration options. Therefore, whenever I add a sub-component to the main Application (via “addChild”), it will also “register” its own configuration options with the ApplicationControlBar menu. This keeps the knowledge of configurability with the containers themselves, yet allows for a more unified means of accessing them.

Thus, when I create each container, I want to instantiate them via their interface so I can guarantee they can register with the ApplicationControlBar. But when I add them to the application, they need to be the base class.

  • 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-11T16:44:13+00:00Added an answer on May 11, 2026 at 4:44 pm

    Okay, I’m anaswering generally, because you said, “Is this really just a phenomenon in Flex/AS3?”.

    In your init method, obviously you’re always calling addChild with foo. That means foo must always be an instance of DisplayObject. You also want it to be an instance of IFooable (though it’s not clear here why). Since DisplayObject is a class, you would consider using a subclass of DisplayObject (e.g. FooableDisplayObject), that implemented IFooable. In Java, this would the below. I’m not familiar with AS, but I think this shows there’s not any general flaw in interfaces here.

    interface IFooable
    {
        public void runFoo();
    }
    
    class DisplayObject
    {
    
    }
    
    abstract class FooableDisplayObject extends DisplayObject implements IFooable
    {
    
    }
    
    class Foo extends FooableDisplayObject
    {
        public void runFoo()
        {
    
        }
    }
    
    public void init()
    {
        FooableDisplayObject foo = new Foo();
        foo.percentHeight = 100;
    
        addChild(foo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

This question is meant to apply to interfaces in general, but I'll use AS3/Flex
I am writing a quote-matching program in which two Abstract Factory Patterns are required,
(If anything here needs clarification/ more detail please let me know.) I have an
I have a large database and want to implement a feature which would allow
I have a 100 classes that have some similar elements and some unique. I've

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.