Skip to main content
Inspiring
April 15, 2013
Answered

A class extends another but imports the same libraries

  • April 15, 2013
  • 2 replies
  • 1216 views

These are imported by the parent class and the child class

import com.Gerry.db.DbManager;

import com.Gerry.controller.Home;

import com.Gerry.data.Data;

One of my classes extends another but both import the same libraries. (I inherited this code). That doesn't make sense does it. By extending a class you inherit all the libraries etc... right?

This topic has been closed for replies.
Correct answer kglad

it makes sense.

class files need to import all the classes used in their scope.

2 replies

Inspiring
April 16, 2013

There is no yes/no answer to this. As kglad said, classes that have references to their datatypes in the current instance must be imported. Nevertheless, if there is no reference to datatypes but just variables - there is no need to import.

Here is an example (see code below). SubClass extends SuperClass. The code demonstrates that:

1. Although SubClass is Sprite - you don't need to import Sprite BUT you can use any Sprite API.

2. Variable movieClip is inherited by SubClass. Although you can use all MovieClip related API - there is no need to import MovieClip class in SubClass.

3. Because TextField is instantiated as distinct vars in both SuperClass and SubClass - TextField must be imported in both.

package 

{

          import flash.display.MovieClip;

          import flash.display.Sprite;

          import flash.text.TextField;

          public class SuperClass extends Sprite

          {

                    public var movieClip:MovieClip;

                    public var superText:TextField;

                    public function SuperClass()

                    {

                              movieClip = new MovieClip();

                    }

 

          }

}

package 

{

          import flash.text.TextField;

          public class SubClass extends SuperClass

          {

                    public var subText:TextField;

                    public function SubClass()

                    {

                              super();

                              addChild(movieClip);

                              movieClip.play();

                    }

 

          }

}

Inspiring
April 16, 2013

Well just to get some more clarification here:

1. You say I can access variables as you have used the super() keyword BUT surely only those declared in the parent/super class constructor.

     I wouldn't be able to access variables declared out of the scope of the constructor ie: in other functions within that class???

2. Variables YES but methods or functions NO. Is that right? ie: To get access to a Classes methods/functions I would have to instantiate it - is that right?

Cheers - this is great learning as all OOP stems from this. I understand more and more every day looking at the basics. I suppose all complex apps are made up of hundreds of simple functions.

Inspiring
April 16, 2013

1. Disregard super() call for now - it is called automatically by subclass.

2. Subclass can access any variables that are declared (even if they are not instantiated) in the scope of the super class as public or protected. Constructor has nothing to do with it.

3. Subclass can access any public and protected methods of super class - similar to properties. As a matter of fact you should try (no matter how counterintuitive it sounds) to mentally blur distinction between methods and properties - they are all just one thing - objects.

I am not sure I actually understood your latest post completely... Need a bit of paella to replenish brain fuel.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 15, 2013

it makes sense.

class files need to import all the classes used in their scope.