InheriTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called InheriTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// InheriTest.java
// Copyright (C) 1998-2016 Davin Pearson
// Website: http://davin.50webs.com/java-tutorials

class Animal
{
   // Properties of the class...
   public int     numberOfLegs;
   public boolean hasWings;

   // Constructor of the class...
   public Animal()
   {
      numberOfLegs = 4;
      hasWings = false;
   }

   // Methods of the class...
   public void talk()
   {
      System.out.println("Hello");
   }
}

class Bird extends Animal
{
   // Properties of the class...
   public boolean canFly;

   // Constructor of the class...
   public Bird()
   {
      super();
      numberOfLegs = 2;
      hasWings = true;
      canFly = true;
   }

   // Methods of the class...
   public void fly() {
      System.out.println("flap flap");
   }
}

class Eagle extends Bird
{
   // Properties of the class...
   private int numberOfKills;

   // Constructor of the class...
   public Eagle()
   {
      super();
      numberOfKills = 0;
   }

   // Methods of the class...
   public void attack()
   {
      numberOfKills++;
   }
}

class InheriTest
{
   // The main method is the point of entry into the program...
   public static void main(String[] args)
   {
      Animal a = new Animal();
      System.out.println(a.numberOfLegs);
      System.out.println(a.hasWings);
      a.talk();
      a.fly();

      Bird b = new Bird();
      System.out.println(b.numberOfLegs);
      System.out.println(b.hasWings);
      System.out.println(b.canFly);
      System.out.println(b.numberOfKills);
      b.talk();
      b.attack();

      Eagle e = new Eagle();
      System.out.println(e.numberOfKills);
      System.out.println(e.numberOfLegs);
      System.out.println(e.hasWings);
      e.talk();
      e.attack();
   }
}

// QUESTIONS:

// (1) Without compiling this file, use your understanding
// of inheritance to say which of the statements in this
// file will not compile and why.  Then use the compiler to
// check that your guesses were correct and comment out all
// the statements with errors in them.

// (2) For each statement that doesn't compile, comment it
// out and beside it write a brief reason why the compiler
// doesn't accept it.

// (3) NOTE: It is essential for the next question that the
// incorrect lines are commented out.

// (4) Compile and run InheriTest to see what is printed
// to the screen.

// (5) Why is the number printed out for "e.numberOfLegs"
// equal to 2 when there is no mention of ever setting the
// value for "numberOfLegs" to 2 in the Eagle class?
//
// HINT: It has something to do with "super".
// 
// (6) Add the following three lines to the main method:

//  a = b;
//  a.talk();
//  a.fly();

// If you compile the code, one of these lines gives a
// compiler error.  Which line is it and what is the reason
// for the error?
// 
// (7) Remove the lines you added from question (6) and add
// the following three lines to the main method:

//  b = a;
//  b.talk();
//  b.fly();

// If you compile the code, one of these lines gives a
// compiler error.  Which line is it and what is the reason
// for the error?
//