0

I am currently new in ActionScript and I am creating an Interface and Class where class implements Interface. I have created Interface and Class both in src/com folder. Here is the code what I did till now.

Interface

package com
{
    public interface TestData
    {
        function getInput(str:String):void
        function getOutput():String
    }
}

Class

package com
{
    public class EntityEL implements TestData
    {
        public var uname:String;

        function getOutput():String
        {
            return uname;
        }

        function getInput(str:String):void
        {
            uname = str;
        }

        public function EntityEL()
        {

        }
    }
}

mxml file

public var etn:EntityEL = new EntityEL();

public function btnClick():void
        {
            etn.getInput(value.text);
            Alert.show(etn.getOutput());
        }


<mx:Button label="Button Click" click="{btnClick();}" />
<mx:TextInput id="value" />

I am getting an error "1044: Interface method getInput in namespace com:TestData not implemented by class com:EntityEL."

Please Help me to solve this problem.

4
  • 2
    try add public to implemented methods in EntityEL class. I thought they had to be public methods when a class implements interfaces. Commented Dec 20, 2011 at 5:27
  • Thanks for the answer. if i declare that public than is there any use of Interface? its just creating a class and call the method of that class. Commented Dec 20, 2011 at 6:04
  • 1
    well, objectives of defining interface differ by each case. In your example here, I don't think you need to create the interface if there is not going to be any other classes that use the interface. I don't think whether it's public or private matters? Commented Dec 20, 2011 at 6:30
  • @SagarRawal Your interface defines how a class can be accessed, but not what the class does. Interfaces can be implemented by many different classes, and one class can implement many interfaces (but extend only one!). Interfaces are essential for keeping your programs loosely coupled, and thus reusable and modular. This is, for example, very important for unit testing, when you're working with more than one developer, or when spreading a program across more than one SWF. Commented Dec 20, 2011 at 6:40

1 Answer 1

3

The idea of an interface is to define a contract between the caller and callee objects: Which methods can be accessed, which parameters are required, and what kind of data will be returned.

In order for that contract to make any sense, these methods have to be accessible, so that the "outside world" is allowed to call them.

When you omit access modifiers, the Flex compiler assumes internal as the default, which means that classes from within the same package have permission to access the methods - and so to some extent, this contract seems fulfilled. The same would be true for any other namespace.

Strangely enough, Adobe clearly doesn't allow it: Your method implementations have to be public.

However, you can declare your interface as internal, so that only classes from the package are allowed to implement it, and that leaves a way to keep your API internal, as well - if that was your intent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.