Reference no: EM132249084
Java Polymorphism
1. Provide a definition for Inheritance-based polymorphism (as opposed to Interface-based polymorphism)
2. Provide a definition for Interface-based polymorphism (as opposed to any Inheritance-based polymorphism)
3. It is possible for methods in separate classes to exist, coincidentally bearing the same name/signature without their having been inherited from another class or interface.
a) Could these also described as "polymorphic" calls?
b) What downsides might we experience in attempting to use them polymorphically?
4. Given only the classes shown in Diagram 1 above: provide their implementations, and complete a working version of the following Java main() method of the Pgm class to correctly determine the total carrying-capacity of a fleet of vehicles. Note: assume the above diagram is complete (no other methods, properties, classes or Interfaces available), and final (no additional methods/properties may be created). You may assume any three unique, positive values x, y, z for the three Automobile-types' capacities.
public class Pgm {
public static void main(String[] args) {
int totalCapacity = 0;
Automobile[] fleet = new Automobile[]{
new Sedan("black"),
new Minivan("blue"),
new SportsCar("red")};
:
:
:
}
}
5. What does the code you had to write in #4 above suggest to you about the limitations of "coincidental" polymorphic methods not based on Inheritance/Interfaces?
Note: before coding up #6, please read all the way to the bottom of these instructions until you find the string: "You may now begin to code"
6. Consider an Automobile class with the following implementations of equals methods:
public boolean equals(Object o) {...}
// Note: override the Object-Class method -- Version A
public boolean equals(Automobile o) {...} // Version B
and a class Sedan that is a subclass of Automobile with the following methods:
public boolean equals(Automobile o) {...} // Version C
public boolean equals(Sedan o) {...} // Version D
and a class Minivan that is a subclass of Automobile with the following methods:
public boolean equals(Automobile o) {...} // Version E
public boolean equals(Minivan o) {...} // Version F
Now suppose you have the following six variables:
Object o = new Automobile();
Automobile auto = new Automobile();
Automobile sedanAuto = new Sedan();
Automobile minivanAuto = new Minivan();
Sedan sedan = new Sedan();
Minivan minivan = new Minivan();
There are 36 ways each of these variables can be paired off in a call to equals, where one of the six is being sent the equals message and one of the six is the argument to equals. For each such pair, indicate which of the equals methods will be executed. For example, if there is a call
auto.equals(auto)
then version B will be called.
List the method-call and result of all 36 calls generated by passing each of the 6 variables above (in the specified order) as arguments (i.e. parameters) to each of the 6 variables, in order. (That is, six separate method-calls to o, followed by six calls to auto, etc.) . Implement each equals() method as a: System.println("Method X") string.
Note: Out-of-order calls are liable to be marked wrong, even if correct!!!
Problem #6 instructions:
- Type out the 36 calls in the correct order as specified (partially demonstrated above)
- On the same line, include your prediction of the printed output for each of the 36 calls
- Implement a main() method that makes the 36 calls in the correct order and run your code
- For each incorrect prediction, explain to me using the 2-step description we went over in class why the Java result is actually correct!
Note: you will not be penalized for incorrect predictions -- only for missing explanations!
The Powerpoint used in-class (lect2pt2_cs3360.ppt) is now in your course INFO folder
- You may now begin to code
Attachment:- Polymorphism.rar