Imagine that you want to add a non-static method to the yee


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 doesn't explicitly extend a class (i.e., it doesn't have an extends clause in its class header). Its class members (i.e., its fields and methods) include:
  • integer fields x and y
  • a non-static method called foo() that takes no parameters and returns a String
  • 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 par
  • a non-static method called moo that takes an integer and returns a String
  • its own toString() method that overrides the inherited one.
  • Class Yee extends Gee. In addition to the members that it inherits, it has:
  • an integer field z
  • its own toString() method that overrides the inherited one.
  • Class Tee extends Yee. In addition to the members that it inherits, it has:
  • a String field called bar
  • an 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 object's fields.
  • 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 getX() and getY().
  • 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 setX() and setY().

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 these classes, although doing so is not required.

  1. The information above states that the Gee class has its own toString() method that overrides the inherited one. Where does the toString() method that Gee overrides come from? Be as specific as possible, and explain your answer briefly.
  2. List all of the fields in a Tee object - both the ones that it declares and the ones (if any) that it inherits.
  3. Consider the following code fragment:
Tee t1 = new Tee();
System.out.println(t1.equals(t1));
System.out.println(t1.foo());
System.out.println(t1);
System.out.println(t1.moo());

Each of the print statements in this code fragment displays the result of a method call. (This includes the third print 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.

Copy the table below into your ps3pr2.txt file, and complete it with appropriate information about each of the method calls. We have filled in the first row for you.

              |              | will the call | if the call compiles,   
  which print | which method | compile       | which version of the    
  statement   | is called    | (yes/no)?     | method will be called?  
=======================================================================
| first one   | equals()     | yes           | the Tee version        |
+-------------+--------------+---------------+------------------------+
| second one  |              |               |                        |
+-------------+--------------+---------------+------------------------+
| third one   |              |               |                        |
+-------------+--------------+---------------+------------------------+
| fourth one  |              |               |                        |
+-------------+--------------+---------------+------------------------+

4. Now imagine that you want to add a non-static method to the Yee class. It should be called yow(), it should take no parameters, and it should return the sum of the integer fields in the called Yee object. Add a full definition of that method to your ps3pr2.txt file. Make sure to take into account the fact that the classes employ proper encapsulation.

5. 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.

  1. Yee y = new Tee();
  2.     Zee z = new Gee();
  3.     Tee t = new Zee();
  4.     Gee g = new Tee();

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Imagine that you want to add a non-static method to the yee
Reference No:- TGS02709986

Now Priced at $10 (50% Discount)

Recommended (94%)

Rated (4.6/5)