Question
Problem 2: Inheritance and polymorphism 18 points total; 2 points each part; individual-only Imagine that you have a set of related classes that have been
Problem 2: Inheritance and polymorphism
18 points total; 2 points each part; individual-only
Imagine that you have a set of related classes that have been defined using inheritance. Here are the key facts about these classes:
Class Gee doesnt explicitly extend a class (i.e., it doesnt have an extends clause in its class header). Its class members (i.e., its fields and methods) include:
- an integer field called c
- a string field called d
- a non-static method called how() that takes no inputs and returns a string
- a non-static method called where() that takes a string and returns an integer
- its own toString() method that overrides the inherited one.
Class Zee extends Gee. In addition to the members that it inherits, it has:
- a string field called a
- its own method called how() that overrides the inherited one
- its own method called why() that takes two integers and returns a double
- its own equals() method that overrides the inherited one.
Class Tee extends Zee. In addition to the members that it inherits, it has:
- a string field called x
- its own method called why() that overrides the inherited one.
- its own toString() method that overrides the inherited one.
Class Yee extends Gee. In addition to the members that it inherits, it has:
- integer fields called e and f
- its own method called where() that overrides the inherited one
- its own equals() method that overrides the inherited one.
In addition, you should make the following assumptions:
- All of the classes employ appropriate encapsulation.
- Each class has a constructor that takes no parameters and initializes the newly created objects fields with their default values.
- Each class includes an appropriate accessor method for each field that is declared in that class. For example, the Gee class would have accessor methods called getC() and getD().
- Each class includes an appropriate mutator method for each field that is declared in that class. For example, the Gee class would have mutator methods called setC() and setD().
Answer the following questions in light of the above information about these classes. Before you begin, you may find it helpful to draw an inheritance hierarchy for the classes, although doing so is not required.
1. (2 points) The information above states that the Gee class has its own toString()method that overrides the inherited one. Where does the toString() method that Geeoverrides come from? Be as specific as possible, and explain your answer briefly.
2. (2 points) List all of the fields in a Tee object both the ones that it declares and the ones (if any) that it inherits. (Note that in our use of the terminology, all of the fields in a superclass are inherited by its subclasses including the private ones.)
3. (5 points) Consider the following code fragment:
Tee t1 = new Tee(); System.out.println(t1.why(5, 10)); System.out.println(t1.how()); System.out.println(t1.where("Waldo")); System.out.println("my Tee is" + t1); System.out.println(t1.equals(t1));
Each of the println statements in this code fragment displays the result of a method call. (This includes the fourth println statement, in which the Java interpreter calls a method on our behalf.) However, it is possible that one or more of these methods calls would fail to compile because the necessary method is neither defined in the Tee class nor inherited from a superclass of Tee.
In section 2-3 of ps4_partI, we have included a table that you should complete with appropriate information about each of these method calls. We have filled in the first row for you, and you should complete the remaining rows.
4. (3 points) Now imagine that you want to add a non-static method to the Yee class. It should be called total(), it should take no parameters, and it should return the sum of all of the integer fields in the called Yee object. Add a full definition of that method to section 2-4 of your ps4_partI file. Make sure to take into account the fact that the classes employ proper encapsulation.
5. (6 points) For each of the following assignment statements, indicate whether it would be allowed according to the rules of polymorphism, and explain briefly why or why not.
a. Gee g = new Tee();
b. Tee t = new Zee();
c. Zee z = new Yee();
d. Object o = new Zee();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started