The BASE for JAVA LEARNING

by PRO FE S S OR


QUIZ for SECTION X

To find an answer, click on the desired question.


QUESTION 1:

Tell one main advantage of Inheritance.

QUESTION 2:

If a derived class inherits features from a super-class, can it inherit other features from another class?

QUESTION 3:

There is a super-class Employee, and there are 3 sub-classes Programmer, Clerk, and Administrator. What's wrong with the following code? Can it be compiled? How can it be corrected?

class Employee
{ // class defined here }

class Programmer inherits Employee
{ // class defined here }

class Clerk inherits Administrator
{ // class defined here }

class Administrator inherits Clerk
{ // class defined here }

QUESTION 4:

Create a Student super-class, and derived classes: Freshman, Sophomore, Junior, and Senior classes. You will also have a separate Date class. The Date class declares a day, month, and year all of the 'int' data type.

The Student class has the following attributes:
  • name as a String Object
  • age as an 'int' variable.

The Freshman class has the following unique attribute:
  • initiationDate of Date class

The Senior class has the following unique attribute:
  • graduationDate of the Date class

Then from the 'main' method, do the following:
  • Instantiate a Freshman class and call the Object "john"
  • Instantiate a Sophomore class and call the Object "carlos"
  • Instantiate a Junior class and call the Object "debbie"
  • Instantiate a Senior class and call the Object "david"

Set the following values for 'john' Object of the Freshman class:
  • age = 15
  • name = "John Smith"
  • initiationDate.day = 14
  • initiationDate.month = 9
  • initiationDate.year = 2001

Set the following value for 'carlos' Object of the Sophomore class:
  • age = 16
  • name = "Carlos Garcia"

Set the following value for 'debbie' Object of the Junior class:
  • age = 17
  • name = "Debbie Byers"

Set the following value for 'david' Object of the Senior class:
  • age = 18
  • name = "David Lynn"
  • graduationDate.day = 15
  • graduationDate.month = 6
  • graduationDate.year = 2002

Have a method called 'display()' in the Student, Freshman and Senior classes.

  • The 'display' method of the Student super-class displays both the name and age values.
  • The 'display' method of the Freshman derived class displays the 'initiationDate' values.
  • The 'display' method of the Senior derived class displays the 'graduationDate' values.

After all variables are set as above, then invoke the display method for each class Object, for example, john.display() invokes the display method for 'john' Object.



In our Answer we are using the 'super' reference in the display method of the Freshman and Senior classes to invoke the display() method of the Student super-class.
























ANSWER to QUESTION 1:

Code Re-usability


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 2:

No; JAVA does not allow for Multiple Inheritance.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 3:

The Clerk and Administrator classes won't compile. Classes cannot inherit from each other, as in the case of the Clerk and Administrator classes.

To correct this, extend Clerk and Administrator classes from Employee.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 4:

Answer not provided yet.


Click your Back Button on your Browser to go back to the QUIZ Section.





Click here to go to the JAVA Mini-Course page