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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:00:41+00:00 2026-05-28T16:00:41+00:00

I have a base class Vehicle and a derived class Car. package Game {

  • 0

I have a base class “Vehicle” and a derived class “Car”.

package Game
{

    public class Vehicle
    {
        public var myVar = "vehicle";

        public function Vehicle()
        {
            trace("vehicle: " + myVar);
            DoSomethingWithMyVar();
        }

    }

}

package Game
{

    public class Car extends Vehicle
    {

        public function Car()
        {
            trace("car pre: " + myVar);
            myVar = "car";
            trace("car post: " + myVar);
            super();
        }

    }

}

Basically, I want to set a vehicle property in Car’s constructor, call Vehicle’s constructor with super() and have Vehicle do something based on myVar. But this is the output i get:

car pre: null
car post: car
vehicle: vehicle

Why does myVar not keep the value of “car” when the base class constructor is called? How am I supposed to implement this correctly?

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

    The variable initialization is in the super class. It won’t be initialized until you actually call super();. This is why you should always call super() before doing anything else in the constructor. In fact, in some other languages, like Java, calls to super() are only allowed as the first statement within subclass constructors.

    However, you should consider using an initialization parameter in your constructor instead of setting the value in the declaration – it is the more elegant and maintainable way of doing what you intend to do.

    package game
    {   
        public class Vehicle
        {
            public var myVar: String;
    
            public function Vehicle ( type:String = "vehicle" ) 
                                 // new Vehicle() traces "vehicle"
            {
                trace("vehicle: " + type);
                myVar = type;
                doSomethingWithMyVar();
            }
        }
    }
    

    resp.

    public function Car ( type: String = "car" )
                                 // new Car() traces "car"
    {
        super( type );
    }
    

    plus, you can always call new Car("Porsche");, or something like that 😉

    P.S. On a side note: Naming conventions in ActionScript recommend using names starting with a lower case letter for both packages and methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class vehicle and some children classes like car, motorbike etc..
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have a base class with an optional virtual function class Base { virtual
I have a model with the following associations: class Car < ActiveRecord::Base belongs_to :transportation_vehicle
I have a base class which adds some functionality to a number of derived
i have base class and several derived classes. how can i resolve these derived
I have base class Class1 and a derived class2. I create two instances of
I have a base class ActivityA that has some dialogs: public static final int
I have base-class Base from which is derived Derived1 , Derived2 and Derived3 .
I have a few classes: class Vehicle { } class Car : Vehicle {

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.