tags:

views:

36

answers:

3

I'm recently getting a bit confused with interfaces and abstract classes and I feel I dont fully grasp it like I thought I did. I think I'm using them incorrectly. I'll describe what I'm doing at the moment, the problem I have faced, and then hopefully it be clear what I'm doing wrong if anything.

I wanted to write some classes that do some parsing of xml. I have different user types that have different parsing requirements.

My logic went as follows.

All parsers share a "parse" function in common and must have at least this function so I made an Interface with this function defined named IParse;

I start out with 2 user types, user type A and user type B. User type A & B share some basic functions but user type B has slightly more functions than A so I put the functions to parse what they share in an abstract class that both will extend called "ParseBase".

So now I have

// Interface

    public interface IParser
     {
      function parse(xml:XML):void;
     }




// Base Class

public class ParseBase()

{
  public function getbasicdata():void{}

public function getmorebasicdata():void{}

}





//User type A

public class userTypeA extends ParseBase implement IParse

{
    public function parse(xml:XML):void
{
       getbasicdata()

       getmorebasicdata()

}


}

//user type B

public class userTypeB extends ParseBase implement IParse

{
    public function parse(xml:XML):void
{
       getbasicdata()

       getmorebasicdata()

}

   public function extraFunctionForB():void
{

}

  public function anotherExtraFunctionForB():void
{

}


}

The problem I have come up against now which leads me believe that I'm doing something wrong is as follows.

Lets say I want to add another function UserTypeB. I go and write a new public function in that class. Then In my implementation I use a switch to check what Usertype to create.

Var userParser:IParser

if(a)

{
userParser= new userTypeA(); 
}else if(b)

{
userParser= new userTypeB();
}

If i then try to access that new function I can't see it in my code hinting. The only function names I see are the functions defined in the interface.

What am I doing wrong?

+2  A: 

You declare the new function only in userTypeB, not in IParser. Thus it is not visible via IParser's interface. Since userParser is declared as an IParser, you can't directly access userTypeB's functions via it - you need to either downcast it to userTypeB, or add the new function to IParser to achieve that.

Of course, adding a function to IParser only makes sense if that function is meaningful for all parsers, not only for userTypeB. This is a design question, which IMO can't be reasonably answered without knowing a lot more about your app. One thing you can do though, is to unite IParser and BaseParser - IMO you don't need both. You can simply define the public interface and some default implementation in a single abstract class.

Oher than that, this has nothing to do with abstract classes - consider rephrasing the title. Btw in the code you show, ParseBase does not seem to be abstract.

Péter Török
I thought an abstract class can be just one that defines functionality but is never directly instansiated?
dubbeat
@dubbeat, a tad more precisely, an abstract class is one that _can not be_ instantiated. You don't tell what language the example is in - to me it looks like Java or C# as well. Java has the `abstract` keyword, and I believe C# too - you should use it :-)
Péter Török
Ah Sorry. Its actually AS3. I think its the only language that doesnt have an "Abstract" Keyword. My Bad... i should of said
dubbeat
I get what an interface is for but I just don't know how to use them properly. I think I'll rid my self of the interface and just extend a base class instead
dubbeat
@dubbeat, I see. Never used AS3 myself, but it seems that at least it has (some) access qualifiers. So lacking `abstract`, you could make the BaseParser constructor(s) `protected` to achieve the same effect.
Péter Török
A: 

In order to access functions for a specific sub-type (UserTypeB, for example) you need the variable to be of that type (requires explicit casting).

The use of interfaces and abstract classes is useful when you only require the methods defined in the interface. If you build the interface correctly, this should be most of the time.

OmerGertel
In your opinion do you think I should be even using a interface for my purpose?How could I achieve explicit casting in the part of my code where I check for user type A or B?
dubbeat
A: 

As Peter Torok says (+1), the IParser declares just one function parse(xml). When you create a variable userParser of type IParser, you will be allowed to call ony the parse() method. In order to call a function defined in the subtype, you will have to explicitly cast it into that subtype.

In that case IMO your should rethink the way you have designed your parsers, an example would be to put a declaration in your IParser (Good if you make this abstract and have common base functionality in here) that allow subtypes (parsers) to do some customization before and after parsing.

You can also have a separate BaseParser abstract type that implemnts the IParser interface.

naikus
What exactly do you mean by "put a declaration" in IParser
dubbeat
Define additional method(s) that you think are absolutely needed by "most" subtypes.
naikus