Class inheritation

Hi,

I don't understand the inheritation: if I have a base class, let's call it circle, which draw's a circle on the screen. I have to initialize the class with the center coordinates. Now, I have a special class which inherits from the circle class, let's call that class circleWithGrid. circleWithGrid brings it's own initizialize() routine with it. How can I initialize the base class?

  • easy just call the initialize of your parent class!

    function initialize() {
    circle.initialize();
    // do optional extra stuff here
    }


    I use inheritance all the time in Monkey C, it's the answer to several issues :)

    If I may also suggest you should have a look at the tutorials on my site ( http://starttorun.info/garmin-connect-iq-tutorial-overview/ ), for instance in the case of the test project I'm also using inheritance there.

  • Thanks Peter. This sounds logical and easy. Nevertheless, let's say, the circle class has as init parameter "center". The CircleWithGrid class would only need as init parameter "color". As I understand you right, I have to define the initialize method of the CircleWithGrid class as follows:

    function initialize (center, color) {
    circle.initialize(center);
    }
  • I haven't had the need to have other parameter signatures in the constructors of my inheritance tree, but I think it should work what you posted, just try it and you'll see :)
  • Former Member
    Former Member over 7 years ago
    If the value of center is static you don't need to include is as a parameter to the intialize() function of CircleWithGrid (but you certainly could if you want).
    var STATIC_CENTER_VALUE = something;

    class CircleWithGrid extends Circle {
    function initialize(color) { // You can define this however you want...
    Circle.initialize(STATIC_CENTER_VALUE); // ...you just need to make sure your parameters to the parent match what the parent expects.
    }
    }
  • Nevertheless, let's say, the circle class has as init parameter "center". The CircleWithGrid class would only need as init parameter "color".


    Yes, if your base class method requires a parameter, you must pass a parameter.

    That said, if all Circles have a center point, and CircleWithGrid is a circle, why wouldn't you need to initialize its center position? It seems logical that you would need to do this.

    Of course you could always just initialize all objects without a center position, and then require that the caller explicitly specify the position later. If you choose this model, it almost makes sense for CircleWithGrid.initialize() to not take any parameters. This makes things consistent.

    class Circle
    {
    hidden var mCenter;
    hidden var mRadius;

    function initialize() {
    }

    function setCenter(center) {
    mCenter = center;
    }

    function getCenter() {
    return mCenter;
    }

    function setRadius(radius) {
    mRadius = radius;
    }

    function getRadius() {
    return mRadius;
    }
    }

    class CircleWithGrid extends Circle
    {
    hidden var mColor;

    function initialize() {
    }

    function setColor(color) {
    mColor = color;
    }

    function getColor() {
    return mColor;
    }
    }


    Another option is to accept the parameter in the base class, and to pass null from the derived class method. This means you'd still have to initialize the center location via an explicit call much like Ken.ConnectIQ has suggested above.

    Travis