Next: Tutorial 17 Arrays inheritance and polymorphism, Previous: Tutorial 15 Introducing inheritance, Up: J.T.W. Tutorials [Contents][Index]
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:
classXWingbeginprivatepropertyint shields;privatepropertyint weapon;privatepropertyboolean dead;constructorXWing()beginshields = 1000; weapon = 10;endmethodint getWeapon()beginreturnweapon;endmethodboolean isDead()beginreturndead;endmethodvoid hit(int damage)beginshields = shields - damage; if (shields<0)thenbeginSystem.out.println("BOOM!!!"); dead =true;endendendclassTieFighterbeginprivatepropertyint shields;privatepropertyint weapon;privatepropertyboolean dead;constructorTieFighter()beginshields = 500; weapon = 20;endmethodint getWeapon()beginreturnweapon;endmethodboolean isDead()beginreturndead;endmethodvoid hit(int damage)beginshields = shields - damage;if(shields<0)thenbeginSystem.out.println("BOOM!!!"); dead = true;endendendclassStarWarsbeginprivatefunctionvoid duel(XWing x, TieFighter t)beginfor (;;)beginx.hit(t.getWeapon());if(x.isDead())thenbeginSystem.out.println("X-Wing is dead");break;endt.hit(x.getWeapon()); if (t.isDead())thenbeginSystem.out.println("Tie Fighter is dead"); break;endendendprivatefunctionvoid battle(XWing good, TieFighter evil)beginvarint g = 0;varint e = 0;varint goodDeaths = 0;varint evilDeaths = 0;while(g<good.length and e<evil.length)beginSystem.out.println("battling X-Wing #" + g + " versus Tie Fighter #" + e); duel(goodg,evile);if(goodg.isDead())thenbeging = g + 1; goodDeaths = goodDeaths + 1;endif(evile.isDead())thenbegine = e + 1; evilDeaths = evilDeaths + 1;endendvarint finalGood = good.length - goodDeaths;varint 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)thenbeginSystem.out.println("The rebel alliance is victorious!");endelsebeginSystem.out.println("The dark side has conquered!");endSystem.out.println();endbeginMain// defines the goodies arrayvarXWing goodies = new XWing3; // initialises the elements of the goodies arraysuperfor(varint i=0togoodies.length-1)begingoodiesi = new XWing();end// defines the baddies arrayvarTieFighter baddies = new TieFighter3; // initialises the elements of the baddies arraysuperfor(varint i=0tobaddies.length-1)beginbaddiesi = new TieFighter();endbattle(goodies,baddies);endMainend
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:
classSpaceShipbeginend
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: Tutorial 17 Arrays inheritance and polymorphism, Previous: Tutorial 15 Introducing inheritance, Up: J.T.W. Tutorials [Contents][Index]