5 Replies Latest reply on Oct 20, 2008 2:38 PM by Newsgroup_User

    Class Not Inheriting Properties of Base Class

    ericbelair Level 1
      Can someone please explain to me why this doesn't work? I have two classes - one extends the other. However, when I cast an instance of the Base Class as the extended Class, the values of the properties are lost. Attached is a condensed version of my code.

      Hope someone can explain to me what I'm doing wrong....

      Thanks.
        • 1. Re: Class Not Inheriting Properties of Base Class
          -Hob Level 1
          You're up-casting. I think if you did trace(specialAccountant) you'd find the entire object is null. There are 2 ways to cast in Flex:

          1) (account as SpecialAccount) - Returns null if it can't perform the cast
          2) SpecialAccount(account) - Throws an exception if it can't perform the cast

          If you took approach 2, you'd get an exception.

          Account cannot be cast to SpecialAccount because it does not contain enough information to be a SpecialAccount. You can only down-cast: (specialAccount as Account).
          • 2. Re: Class Not Inheriting Properties of Base Class
            Level 7

            "ericbelair" <webforumsuser@macromedia.com> wrote in message
            news:gdis43$5b0$1@forums.macromedia.com...
            > Can someone please explain to me why this doesn't work? I have two
            > classes -
            > one extends the other. However, when I cast an instance of the Base Class
            > as
            > the extended Class, the values of the properties are lost. Attached is a
            > condensed version of my code.
            >
            > Hope someone can explain to me what I'm doing wrong....
            >
            > Thanks.
            >
            > /* Base Class */
            > package
            > {
            > [Bindable]
            > public class Account
            > {
            > public var name:String = "";
            >
            > public function Account()
            > {
            >
            > }
            > }
            > }
            >
            > /* Extended Class */
            > package
            > {
            > [Bindable]
            > public class SpecialAccount extends Account
            > {
            > public var otherProp:Boolean
            >
            > public function SpecialAccount()
            > {
            > super();
            > }
            > }
            > }
            >
            > /* Then, in another package, I call the following function. */
            >
            > public function setupAccounts():void // Not a contructor
            > {
            > var account:Account = new Account();
            >
            > account.name = "new name";
            >
            > var specialAccount:SpecialAccount = account as SpecialAccount;
            >
            > trace(specialAccount.name) // null
            > }

            You can cast a SubClass as a base class, but not the reverse.

            Consider if you have a Rectangle base class and a Square subclass. Can you
            cast a Square to a Rectangle? Yes, because all Squares are Rectangles. Can
            you cast a Rectangle as a Square? No, because the Rectangle might or might
            not be a Square. It quite probably is long and thin.

            The only time you can "up" cast like that is if you have first "down" cast.
            For instance, you might create an XMLListCollection and house it in a
            variable that is a ListCollectionView so that you can handle things like
            filtering, etc., in a generic manner. If you need something specific to
            XMLListCollection, then you can cast that variable as XMLListCollection in
            order to get access to it without a compiler error. This works because the
            object housed in a variable typed as the parent class actually _is_ an
            instance of the child class to begin with.

            HTH;

            Amy


            • 3. Re: Class Not Inheriting Properties of Base Class
              ericbelair Level 1
              won't it throw an error if i do it the other way around too? if i were to cast a SpecialAccount as an Account, won't it fail because SpecialAccount has a property that Account does not?
              • 4. Re: Class Not Inheriting Properties of Base Class
                ericbelair Level 1
                So, let's say that I have two types of Accounts. Each has properties that the other does not, but they each share several properties. What's the best way to handle this? Create all three as separate classes, each with all of the shared properties duplicated? Or one class that has all of the properties?
                • 5. Re: Class Not Inheriting Properties of Base Class
                  Level 7

                  "ericbelair" <webforumsuser@macromedia.com> wrote in message
                  news:gditfr$6sf$1@forums.macromedia.com...
                  > So, let's say that I have two types of Accounts. Each has properties that
                  > the
                  > other does not, but they each share several properties. What's the best
                  > way to
                  > handle this? Create all three as separate classes, each with all of the
                  > shared
                  > properties duplicated? Or one class that has all of the properties?

                  There are several ways you can do this. Probably the most straightforward
                  is to create an "Abstract class" that is never intended to be instantiated,
                  that you then extend for each of your concrete classes. You could also
                  write an interface that defines your common properties that you then
                  implement in each class.

                  You may want to invest in a copy of ActionScript 3 Design Patterns. It was
                  written to address these types of questions.

                  HTH;

                  Amy