GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      
      Tutorial 1             Tutorial 2               Tutorial 3                   Tutorial 4                 Tutorial 5      
      Tutorial 6             Tutorial 7               Tutorial 8                   Tutorial 9                 Tutorial 10      
      Tutorial 11             Tutorial 12               Tutorial 13                   Tutorial 14                 Tutorial 15      
      Tutorial 16             Tutorial 17             Tutorial 18              Using Emacs            Download Links    


J.T.W. tutorial 16: Advanced inheritance


§ 16 Tutorial 16

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 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(good[g],evil[e]); 
         if (good[g].isDead()) 
         then begin 
            g = g + 1; 
            goodDeaths = goodDeaths + 1; 
         end 
         if (evil[e].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:"   + finalGood   + "  " + 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 XWing[3]; 

      // initializes the elements of the goodies array 
      for (var int i=0; i<goodies.length; i = i + 1) 
      begin 
         goodies[i] = new XWing(); 
      end 

      // defines the baddies array 
      var TieFighter[] baddies = new TieFighter[3]; 

      // initializes the elements of the baddies array 
      for (var int i=0; i<baddies.length; i=i+1) 
      begin 
         baddies[i] = new TieFighter(); 
      end 

      battle(goodies,baddies); 

   endMain 
end 

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

Question 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 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 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 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 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 16.8: Compile and run your program again, making sure that it produces the same results now that it is using inheritance.

Question 16.9: The SpaceShip class is a superclass 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.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Tue Apr 18 20:33:48 NZST 2017
Best viewed at 800x600 or above resolution.
© Copyright 1999-2017 Davin Pearson.
Please report any broken links to