In the introduction section context related to the previous sessions is established. Teacher introduces the concept ofinheritance. Teacher should make this concept more relatable to students by using real-life examples (e.g., if parent-child relation is considered, children inherit characteristics from their parents, like hair type, eye color, etc.). The benefits of inheritance should be discussed. These concepts are considered in the context of the Greenfoot Environment and Java programing language.
Teacher introduces the class hierarchy in the context of inheritance concept. Teacher introduces ancestor class (also known as: super class, parent class) and descendant classes (also known as: subclasses, child classes):
o Previously examined real-life classes can be discussed in this context,
o In this context, it should be discussed that subclasses can inherit properties (i.e., attributes and methods) from theparent class,
o However, it should be discussed that subclasses can incorporate additional properties not available in the parent class,
Benefits of the class hierarchy in the context of inheritance conceptshould be discussed. It should be explained that in Java programing language each class can have multiple subclasses, but only one parent class. The role of the Object class in the context of class hierarchy and inheritance can be discussed.
Based on the identified common properties, new class PassiveActor containing act() method should be implemented:
o These classes (PassiveActor, Orb, and Direction) should be used for representing class hierarchy in the context of inheritance,
o Teacher can visually represent class hierarchy by using the hierarchy diagram.
o Teacher alerts the students what changed in the Greenfootenviroment when Actor in substituted with PassiveActor in the class
The concept of abstract class is introduced by the teacher. It should be discussed that abstract classes serve as blueprints for other classes and cannot be instantiated. However, they are essential in designing class hierarchies. Real-world examples related to abstract classes and subclasses can be discussed by the teacher and students (e.g., class Computer with basic properties can be defined as an abstract class, and can be specialized to Console, Desktop, Laptop, and Mobile Phone, each with a specific set of properties, etc.). Another example could be geometric figures. Rectangle or triangle can be inherited from abstract class figure. When calculation girt and area of general figure we do not have exact formula. But we have exact formula for rectangle and triangle. Square can be inherited from rectangle. Students should discuss more examples of geometric figures and bodies.
Definition of an abstract class in the game.
The concept of abstract class is considered in the Greenfoot Environment and Java programing language. In the context of game development, the PassiveActor class is a blueprint for acting. Therefore, it is defined as an abstract class and established as the ancestor of the Orb and Direction classes, making Orb and Direction its descendants. Since the act() method is already defined in the PassiveActor class, it should be removed from the Orb and Direction classes.
Definition of an abstract class related to entity movement.
Based on the identified common properties, new abstract class MovingActor containing act() method should be implemented. Additionally, MovingActor is established as the ancestor of the Bullet and Enemy classes, making Bullet and Enemy its descendants. It should be discussed that the subclasses inherit common properties from the parent class.TheMovingActor class is a blueprint for class design and should be declared as an abstract.
Identification of class-specific properties related to entity movement.
Class-specific properties related to entity movement are examined. The act() method of respective classes is investigated, as well as the attributes moveDelay and nextMoveCounter. It can be observed that code of act() method responsible for movement is the same.
The teacher introduces the super keyword. The super keyword in the context of inheritance was introduced:
o super keyword can be used in order to invoke constructor from the parent class,
o super keyword can be used in order to invoke method from the parent class,
o super keyword can be used in order to invoke attribute from the parent class,
o super must be first statement
Benefits of using the super keyword in the context of inheritance should be discussed.
It should be noted that there is a situation, in which this is true - i. e. in contructor.
Refactoring code related to entity movement.
Code refactoring related to entity movement was performed. Previously identified attributes moveDelay and nextMoveCounter from Bullet and Enemy subclasses are moved to the ancestor class MovingActor. Parametric constructor to initialize these attributes is defined in MovingActor class. This constructor with proper parameters was invoked from the Bullet and Enemy subclasses using the super keyword. The code responsible for movement in act() method of subclasses Bullet and Enemy was moved to act() method of MovingActor class, while the rest of the implementation remains unchanged in the subclasses. Finally, parent version of method act() is invoked as first line of method act() in subclasses Bullet and Enemy.It should be discussed that subclasses can incorporate additional properties not available in the parent class (i.e., different implementation of act() method).
Creation of custom enemies.
The focus in on the Enemy class and definition of additional subclasses representing different enemies (e.g., Frog and Spider). Images and parameterless constructors (with appropriate invocation of the parent constructor) should be defined for each enemy type.
The Liskov Substitution Principle is introduced. This principle is part of the SOLID principles of object-oriented design. The principle states that functions that use pointers or references to parent classes should be able to use objects of subclasses. Real-word examples should be discussed (e.g., if Computer class is defined as the parent class, and Console, Desktop, Laptop, and Mobile Phone classes are defined as subclasses, the Liskov Substitution Principle says that functions which are using Computer class will also work with all subclasses, without any change in the code). Benefits of using the Liskov Substitution Principlein the context of inheritance should be discussed.
Spawning of custom enemies.
The task is dedicated to spawning custom enemies. The Arena.spawn() method is examined, and custom enemies are created through various decisions and stored in a variable of type Enemy. It should be observed that no other code in the application needs to be changed, demonstrating the application of the Liskov Substitution Principle.
Discuss hierarchy of Arenas.
The Arena class hierarchy is discussed,It should be observed that the subclasses of Arena are responsible for custom layouts (e.g., positions of Orb and Direction instances, size of the arena). These tasks are performed in the constructors of the subclasses, which set and store spawning positions, rotations, and dimensions of the arena.
Make universal Arena.
Based on the previous discussion, a universal Arena class is introduced. Additional attributes (spawnPositionX, spawnPositionY, and spawnRotation) are defined, initialized in the constructor, and used in the spawn() and respawn(Enemy) methods. Attributes related to the arena's dimensions (width and height) are also defined and initialized in the constructor. As the Arena class serves as a blueprint for defining concrete arenas, it is defined as an abstract class.
Create DemoArena.
Based on the identified Arena class, the DemoArena subclass is defined. The DemoArena constructor is defined, invoking the parent class constructor, and the code responsible for the layout of directions, orbs, and towers is moved from the Arena constructor to the DemoArena constructor. Finally, a new instance of the DemoArena class is created.To activate Demoarena, right click and select new DemoArena.
The concept of inheritance is reviewed. Benefits of inheritance are reviewed. The class hierarchy and its benefits in the context of inheritance concept are discussed. The concept of abstract class is reviewed. The super keyword in its benefits in the context of inheritance are discussed. The Liskov Substitution Principle is reviewed and benefits of using the Liskov Substitution Principlein the context of inheritance are discussed. Real-life inheritance examplesarediscussed.Game-related inheritance examples and implementation are discussed.