InheriTest.java Answers

Click here to go back to the questions
// ANSWERS:

// (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".
// 
//
// ANSWER: The call of "super" in each of the constructors
// causes the constructor of the immediate superclass to be
// called.  Therefore a call to the Eagle constructor
// results in calls to the Animal constructor and the Bird
// constructor, in that order.
//
// 
// (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?
// 
//
// ANSWER: The line that says "a.fly()" will not compile, because
// the reference "a" is of compile-time type "Animal" and the
// Animal class contains no such method "fly".
//
// 
// (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?
// 
//
// ANSWER: The line that says "b=a" will not compile,
// because the reference "a" is of compile-time type
// "Animal" and the reference "b" is of compile-time type
// "Bird".  Because of the non-symmetrical nature of
// inheritance, you are not allowed to copy an Animal
// reference to a Bird reference.
//
//