Next: , Previous: , Up: J.T.W. Tutorials   [Contents][Index]


4.16 Tutorial 16 More inheritance

This tutorial shows you a practical example of inheritance. The file StarWars.jtw is comprised of three classes: XWing, TieFighter and StarWars. The first two represent spacecraft from the two sides of the Star Wars films. The class StarWars is the driver class and contains code for executing a battle between the X-Wings and the Tie Fighters.

Question 4.16.1: Study, compile and run the following code:

class XWing
begin

    private property int     shields;
    private property int     weapon;
    private property boolean dead;

    constructor XWing()
    begin
        shields = 1000;
        weapon  = 10;
    end

    method int getWeapon()
    begin
        return weapon;
    end
    method boolean isDead()
    begin
        return dead;
    end
    method void hit(int damage)
    begin
        shields = shields - damage;
        if (shields<0)
        then begin
            System.out.println("BOOM!!!");
            dead = true;
        end
    end
end

class TieFighter
begin

    private property int     shields;
    private property int     weapon;
    private property boolean dead;

    constructor TieFighter()
    begin
        shields = 500;
        weapon  = 20;
    end

    method int getWeapon()
    begin
        return weapon;
    end
    method boolean isDead()
    begin
        return dead;
    end
    method void hit(int damage)
    begin
        shields = shields - damage;
        if (shields<0)
        then begin
            System.out.println("BOOM!!!");
            dead = true;
        end
    end
end

class StarWars
begin

    private function void duel(XWing x, TieFighter t)
    begin

        for (;;)
        begin
            x.hit(t.getWeapon());
            if (x.isDead())
            then begin
                System.out.println("X-Wing is dead");
                break;
            end
            t.hit(x.getWeapon());
            if (t.isDead())
            then begin
                System.out.println("Tie Fighter is dead");
                break;
            end
        end
    end

    private function void battle(XWing good, TieFighter evil)
    begin
        var int g          = 0;
        var int e          = 0;
        var int goodDeaths = 0;
        var int evilDeaths = 0;

        while (g<good.length and e<evil.length)
        begin
            System.out.println("battling X-Wing #" + g + " versus Tie Fighter #" + e);
            duel(goodg,evile);
            if (goodg.isDead())
            then begin
                g = g + 1;
                goodDeaths = goodDeaths + 1;
            end
            if (evile.isDead())
            then begin
                e = e + 1;
                evilDeaths = evilDeaths + 1;
            end
        end

        var int finalGood = good.length - goodDeaths;
        var int finalEvil = evil.length - evilDeaths;

        System.out.println();
        System.out.println("Battle Report:     X-Wings     Tie Fighters");
        System.out.println("----------------------------------------------");
        System.out.println();
        System.out.println("Initial ships:" + good.length + "  " + evil.length);
        System.out.println();
        System.out.println("Killed ships:"  + goodDeaths  + "  " + evilDeaths);
        System.out.println();
        System.out.println("Final ships:"    + finalGoodPD + "  " + finalEvil);
        System.out.println();
        if (finalGood>finalEvil)
        then begin
            System.out.println("The rebel alliance is victorious!");
        end
        else begin
            System.out.println("The dark side has conquered!");
        end
        System.out.println();
    end

    beginMain
        // defines the goodies array
        var XWing goodies = new XWing3;
        // initialises the elements of the goodies array
        superfor (var int i=0 to goodies.length-1)
        begin
            goodiesi = new XWing();
        end

        // defines the baddies array
        var TieFighter baddies = new TieFighter3;
        // initialises the elements of the baddies array
        superfor (var int i=0 to baddies.length-1)
        begin
            baddiesi = new TieFighter();
        end

        battle(goodies,baddies);

    endMain
end

Question 4.16.2: Compile and run this file to see the battle between the X-Wings and the Tie Fighters unfold.

Question 4.16.3: If you look at the Java code for the XWing and TieFighter classes you will notice that they are almost identical: They have the same methods and properties, the only difference is that the XWing objects are initialized with a different value for their shields and weapon properties to the TieFighter objects.

The next few questions will guide you through the process of using inheritance to eliminate this unnecessary duplication of code. A new class called SpaceShip will be created and all of the code that is common to XWing and TieFighter will be moved into this class. The XWing and TieFighter classes will then be modified so that they both inherit from SpaceShip.

Question 4.16.4: The first step in this process is to create the outer shell of the SpaceShip class, which you should now type in:

class SpaceShip
begin
end

Question 4.16.5: Move the properties shields, weapon and dead out of the XWing and TieFighter classes and into the SpaceShip class. You must change the privacy status of the properties from private to protected. The protected modifier was invented as an intermediate level of privacy between public and private. Like private, it allows visibility to the same class in which the method or property was defined, but unlike private it also allows visibility to sub-classes of the class in which the method or property was defined.

Question 4.16.6: Move the three methods getWeapon, isDead and hit out of the XWing and TieFighter classses and into the SpaceShip class. At this point, the XWing and TieFighter classes should contain nothing but a constructor.

Question 4.16.7: Finally, add the extends keyword to the first line of the XWing and TieFighter classes:

class XWing extends SpaceShip)

and

class TieFighter extends SpaceShip)

Question 4.16.8: Compile and run your program again, making sure that it produces the same results now that it is using inheritance.

Question 4.16.9: The SpaceShip class is a super-class of both XWing and TieFighter containing everything that X-Wings and Tie Fighters contain in common. Because the role of the SpaceShip class is simply to hold these commonalities, we might choose to label the class with the abstract keyword:

abstract class SpaceShip)

This prevents us from creating instances of the SpaceShip class. Without the abstract modifier, we could happily create a new SpaceShip(), which would be an object that is not an X-Wing, nor a Tie Fighter, but just a vague "space ship". If we consider this to be a logical mistake then we can use abstract to prevent such calls to the SpaceShip constructor. Change the class SpaceShip to be abstract and observe how the compiler will not accept any lines of the form:

var SpaceShip s = new SpaceShip();) // compiler error

Remove the abstract keyword and notice how the compiler will then allow this line to compile.


Next: , Previous: , Up: J.T.W. Tutorials   [Contents][Index]