Matching the superclass's constructor's parameter list, is treating a null default value as a non-null value within a constructor a violation of LSP?

Posted by Panzercrisis on Programmers See other posts from Programmers or by Panzercrisis
Published on 2014-06-13T02:14:04Z Indexed on 2014/06/13 3:39 UTC
Read the original article Hit count: 300

I kind of ran into this when messing around with FlashPunk, and I'm going to use it as an example.

Essentially the main sprite class is pretty much class Entity. Entity's constructor has four parameters, each with a default value. One of them is graphic, whose default value is null.

Entity is designed to be inherited from, with many such subclasses providing their own graphic within their own internal workings. Normally these subclasses would not have graphic in their constructor's parameter lists, but would simply pick something internally and go with it.

However I was looking into possibly still adhering to the Liskov Substitution Principal. Which led me to the following example:

package com.blank.graphics 
{
    import net.flashpunk.*;
    import net.flashpunk.graphics.Image;

    public class SpaceGraphic extends Entity
    {
        [Embed(source = "../../../../../../assets/spaces/blank.png")]
        private const BLANK_SPACE:Class;

        public function SpaceGraphic(x:Number = 0, y:Number = 0, graphic:Graphic = null,
                mask:Mask = null)
        {
            super(x, y, graphic, mask);
            if (!graphic)
            {
                this.graphic = new Image(BLANK_SPACE);
            }
        }
    }
}

Alright, so now there's a parameter list in the constructor that perfectly matches the one in the super class's constructor. But if the default value for graphic is used, it'll exhibit two different behaviors, depending on whether you're using the subclass or the superclass. In the superclass, there won't be a graphic, but in the subclass, it'll choose the default graphic.

Is this a violation of the Liskov Substitution Principal? Does the fact that subclasses are almost intended to use different parameter lists have any bearing on this? Would minimizing the parameter list violate it in a case like this? Thanks.

© Programmers or respective owner

Related posts about coding-standards

Related posts about inheritance