Java Chapter 9 - Inheritance

9.1 Inheritance Basics

Classes allow you to create objects with both properties (variables) and methods.  Inheritance allows you to create a class (subclass) that inherits the properties of another class (superclass) using the extends command.  It allow you to create the IS-A relationship or parent-child relationship.

class Animal
{
  
// properties and methods that apply to ALL animals - e.g. Weight
}

class Bird
extends Animal
{
  
// properties and methods that apply only to birds - e.g. Wingspan
}

class Mammal
extends Animal
{
  
// properties and methods that apply only to mammals
}

When you create an instance of a Bird or Mammal in your main program, it will have the properties and methods defined in the Bird or Mammal class in addition to the properties and methods defined in the Animal class:

Bird Polly = new Bird(); 
// Polly has all the properties and methods of classes Bird and Animal

The program below illustrates inheritance by creating a GameObject superclass and Player and Enemy subclasses.  One player (Frodo) and two enemies (Orc and Troll) are created.

Inheritance Example

public class game
{
   public static void main(String[] args)
   {
      Player Frodo = new Player(1,1,"Bill");

      Enemy A = new Enemy(8,12,"Orc");
      Enemy B = new Enemy(15,18,"Troll");
   }
}

class GameObject  
// superclass
{
   int X, Y;
}

class Player
extends GameObject   // subclass
{
   int Health = 100;
   int Strength = 10;
   String Name;

   Player(int newX, int newY, String newName)
   {
      X = newX; Y = newY; Name = newName;
   }
}

class Enemy
extends GameObject   // subclass
{
   int Health = 100;
   int Strength; // Strength value depends on Race
   String Race;

   Enemy(int newX, int newY, String newRace)
   {
      X = newX; Y = newY; Race = newRace;

      if (Race.equals("Orc"))
         Strength = 15;
      if (Race.equals("Troll"))
      Strength = 40;
   }
}


9.2 Data Hiding with Protected

Classes provide the public and private modifier for variables and methods.  This allows the programmer to make all modifications of variables go through an interface, or accessor method (see section 8.3).  Inheritance adds the protected modifier for variables and methods in the superclass.  This allows the variable or method to be accessed within the superclass, any subclass, but not directly from the main program.

class GameObject
{
  
protected int X, Y;  // protected allows access from this class, all subclasses, but not the main program
}

9.3 Keywords Super and This

The "super" keyword refers to the parent class.  It may be used in these ways:

1.  super.variable_name - refers to a variable in the parent class
2.  super.method_name - refers to a method in the parent class
3.  super() - refers to the constructor in the parent class
 

Super and This Example

public class superthis
{
   public static void main(String[] args)
   {
      Bike Ninja = new Bike();
      Ninja.Display();
   }
}

class Vehicle
{
   int Speed=100;
}

class Bike extends Vehicle
{
   int Speed=150;
 
   void Display()
   {
      System.out.println(this.Speed);
  //prints speed of Vehicle
      System.out.println(super.Speed);
//prints speed of Bike
   }
}

Output

150
100