0
votes

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.

1
try add public to implemented methods in EntityEL class. I thought they had to be public methods when a class implements interfaces.mask8
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.Sagar Rawal
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?mask8
@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.weltraumpirat

1 Answers

3
votes

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.