PowerPoint
THIS IS THE POWER POINT PRESENTATION IN TEXT FORM. UNABLE TO LOAD THE POWER POINT ONTO WEB SPACE AT THE MOMENT.
Polymorphism Showdown
Davis Dulin,
James Creager, and Ian Charles
Continue to Overview
Skip to Instructions
Skip to Questions
Polymorphism
Polymorphism is the ability of a subclass to define new or share functionality with a superclass.  A subclass automatically has possession of public and protected variables and methods in its superclass.  These inherited variables and methods can be called directly from an object of the subclass without having to change type.
 
Overriding
It is also possible to redefine variables and methods within a subclass.  In such a case, a variable/method defined in the subclass overrides the variable/method with the same name in the superclass.  When called from an instance of the subclass, the variable/method defined in the subclass will be accessed instead of the one in the superclass.
Overloading
Overloading is a technique by which a method takes on many forms.  Overloading occurs when a method name is used multiple times, but each instance of the method contains different parameters.  This allows multiple variations of the same action to occur depending on circumstances.
Casting
Polymorphism also allows objects of one class to be transformed into objects of another class.  This is particularly useful when changing functionality in an inheritance relationship.  An instance of the superclass’ type can be set equal to an instance of the subclass’ type without any conflicts in the program.  This is because the subclass is a more specific class of the superclass.  Moving from subclass to superclass, however, would be making the subclass more generic.  A technique known as casting is required to perform this change. Casting objects is only valid when moving up an object’s hierarchy (up to the superclass).  Casting cannot be used between objects on the same level of the object hierarchy, and casting is not necessary to move down the object hierarchy.  In order to cast, you place a superclass’ name in parentheses just before the object you want to transform. By doing this, you are declaring a new type.
How to Play
Open the GoogleDoc form.  See instructor for web address.
Fill out the form except for the answer blank.
The instructor will change to the slide containing the question.
Submit a correct answer in the form as quickly as possible.
After 1min the instructor will call time, reveal the answer slide, and determine winners.
 
How to Play
Every numbered question has a showdown question (S) that follows.
Points are only earned from showdown questions.
Everyone competes in numbered questions.  Selected winners compete in showdown questions.
How To Play
For numbered questions:  The first person to provide a correct answer from each row in the classroom will advance to the showdown.  If a row does not submit a correct answer, no player advances from that row.
 
For showdown questions:  The first person to provide a correct answer earns a point for his/her row.  The instructor will tally the score on the board.
 
The row with the most points at the end of the questions wins.
How To Play
You may be asked to:
identify something as legal or illegal
write out variable declarations
identify the type of polymorphism
 
NOTE: "illegal" may be the answer at any time For showdown questions:  The first person to provide a correct answer earns a point for his/her row.  The instructor will tally the score on the board.
A slide demonstrating the layout follows.
Question # and Category Will Be Here
Question Will Be Here
If Included, Diagram Will Be Here
Upcoming Question Will Be Here
Question 1 follows.
Q1 - Casting
consider a class named Transportation, with subclasses named Bus, Car, and Bike.  If you have an instance of Car created, how could you cast that Car instance to be of type Bus?
Answer to Question 1 follows.
Q1 - Answer
illegal
Question 1 Showdown follows.
Q1S -
ADDED SOME MORE.  TURNED OUT TO BE EASY SINCE REPETITIVE.  SHOULD SHOOT FOR 15.
OK, SOUNDS GOOD
Answer to Question 1 Showdown follows.
Q1S - Answer
 
Question 2 follows.
Q2 - Casting
If BMW is a subclass of Car then how would you cast myCar, an instance of Car, to be of type BMW?
answer: myCar = (BMW) car
Answer to Question 2 follows.
Q2 - Answer
 
Question 2 Showdown follows.
Q2S -
 
Answer to Question 2 Showdown follows.
Q2S - Answer
 
Question 3 follows.
Q3 - Casting
An Animal superclass exists with subclasses Wolf and Gator.  Objects of each have been instantiated (a, w, and g respectively).  Which of the following is legal?
 
a)  w = (Wolf) g;
b)  w = g;
c)  a = g;
d)  w = a;
Answer to Question 3 follows.
Q3 - Answer
c)  a = g;
 
No cast is necessary when moving down the object hierarchy.
Question 3 Showdown follows.
Q3S - Casting
Following the same premise:  An Animal superclass exists with subclasses Wolf and Gator.  Objects of each have been instantiated (a, w, and g respectively).
 
Is the following legal?
g = (Gator) a;
Answer to Question 3 Showdown follows.
Q3S - Answer
yes
When moving up the object hierarchy, a cast is necessary.
Question 4 follows.
Q4 - Casting
2) Given the UML diagram, how would you declare "athlete" of type Athlete as "sp" of type SoccerPlayer?
Q4 - Answer
athlete = sp;
No cast is needed when moving down the object hierarchy.
Q4S - Overriding
SoccerPlayer and FootballPlayer both have a kick method, neither of which has a parameter.  Is this an example of overriding?
Q4S - Answer
no
The FootballPlayer class does not inherit a kick method to override.  The same applies to SoccerPlayer.
Q5 - Overloading/Overriding
Is this an example of overloading, overriding, both, or neither?  All methods in the diagram do not have parameters.
Q5 - Answer
overriding
Transformer inherits shoot and move methods, but then defines its own.
Q5S - Overloading
In one line, define a method for the Transformer class that overloads shoot.  All methods in the diagram do not have parameters.
Q5S - Answer
___ void shoot( ___  ___ ){}
Any parameter will work because the previously defined method has no parameter.
Override
 
Q5 - Inheritance
Inheritance plays an important role with regards to the restrictions of polymorphism.  From the following examples, state which ones would be most efficiently implemented through inheritance:
(A) There are two main kinds of sandwiches that have many properties in common: a vegetarian sandwich and a sandwich with meat
(B) A grocery cart contains lots of different items
(C) A megazord has a lot of smaller zoid's
(D) Lots of people apply for the same job
(E) Types of transportation include bikes, cars, and kodo's
A - 5
Answer:  A, E
Q7 - Casting
A superclass, SuperSaiyan, has a subclass SuperSaiyan2, which has a subclass SuperSaiyan3.  These are instantiated as "ss", "ss2", and "ss3" respectively.  How can the SuperSaiyan2 object be declared as a SuperSaiyan?
Q7 - Answer
ss2 = (SuperSaiyan2) ss;
Casting is required when moving up the object hierarchy.
Q7S - Overloading/Overriding
Following the same premise:  A superclass, SuperSaiyan, has a subclass SuperSaiyan2, which has a subclass SuperSaiyan3.  These are instantiated as "ss", "ss2", and "ss3" respectively.  
 
SuperSaiyan has the method void increasePower(int i).  SuperSaiyan2 has the method void increasePower(int i, int j).  SuperSaiyan3 has the method void increasePower(int i, int j, int k).
 
Is this an example of overloading, overriding, both, or neither?
Q7S - Answer
overloading
Although method names are common, the parameters differ.  Subclasses will have access to both their own increasePower method and the superclass increasePower method(s). 
A - 7
Answer: FREEEZAAAA KILLZZZ ALLLLLL ARRRGGGGG
 
But the scouter says over 9000! haha i wish i could remember wut that meant :(
Q7- Overloading
Does the following code implement overloading? If so, how? If not, what is wrong with the code?
public int deposit(int accountNumber, double depositAmount){
    ...
}
public double deposit(int accountNumber, double depositAmount){
    ...
}
Q7 Answer
No, the code doesn't utilize overloading because even though the return types are different, the parameters are the same.  
Q8- Casting
fill in the blank to contruct an instance of Internet :
public class Wireless extends Internet{
    Wireless wifi;
    Internet router1 =  _______ wifi;
Q8- Answer
Internet router1 =  (Internet) wifi;
Q9
fix the error relating specifically to overloading:
public interface Drivable{
    public double accelerate(){
    ..... 
    return newSpeed;
    }
}
public class Bike extends Drivable{
    Bike tricycle;
    double newSpeed;
    newSpeed = tricycle.accelerate(double speedChange); 
}
showdown question: fix the other error (the answer is not related to constructors
Q9 Answer
Answer: turn the accelerate method in Drivable to:
    public double accelerate(double speedChange){
    ...
    return newSpeed;
    }
Showdown Answer: create a method in the Bike class into which the method call tricycle.accelerate(...) can be written
Q10
 
 
What must be true if a child of an abstract parent class does not override all of the parent's abstract methods?
a.    This is always an error.
b.    The child class itself must be declared to be abstract.
c.    Child classes are automatically non-abstract, so this is OK.
d.    The parent class is in error.
A9
B
Q10S
What must a non-abstract child do about an abstract method in its parent class?
a.    A child must override an abstract method inherited from its parent by defining a method with the same signature and same return type.
b.    A child must define an additional method similar to the one inherited from its parent by defining a method with the same signature and different return type.
c.    A child must not define any method with the same signature as the parent's abstract method.
d.    A non-abstract child must define an abstract method with the same signature and same return type as the parent's abstract method. 
A10S
A
Q11
What is the apparent type of an object?  
  A. The type of the expression that uses the object (your answer)
B. The result of the toString() method    
C. the subtype of the object  
D. the class used to instantiate the object
Q11A
A
Q11S
An apparent type is always what?
  A. a subtype of the actual type    
B. the same for each instance (your answer)   
C. a supertype of the actual type (correct answer)  
  D. None of the above
Q11SA
C
Q12
 
 
 
Say that class Rodent has a child class Rat and another child class Mouse. Class Mouse has a child class MiniMouse. Examine the following
    Rodent rod;
    Rat rat = new Rat();
    Mouse mos = new Mouse();
    MiniMouse min = new Minimouse();
Which one of the following will cause a compiler error?
a.    rod = rat; b.    rod = mos; c.    min = null; d.    min = rat;
Q12A
D
Q12S
Say that the situation is the same as in the previous question. Which of the following array declarations is correct for an array that is expected to hold up to 10 objects of types Rat, Mouse, and MiniMouse?
a.    Rat[] array = new Rat[10];
b.    Rodent[] array = new Rat[10];
c.    Rodent[] array = new Rodent[10];
d.    Rodent[10] array;
Q12SA
C
Q13
What must a non-abstract child do about an abstract method in its parent class?
a.    A child must override an abstract method inherited from its parent by defining a method with the same signature and same return type.
b.    A child must define an additional method similar to the one inherited from its parent by defining a method with the same signature and different return type.
c.    A child must not define any method with the same signature as the parent's abstract method.
d.    A non-abstract child must define an abstract method with the same signature and same return type as the parent's abstract method.
Q13A
A
Q13S
 
 
What must be true if a child of an abstract parent class does not override all of the parent's abstract methods?  
 
a.    This is always an error. b.    The child class itself must be declared to be abstract. c.    Child classes are automatically non-abstract, so this is OK. d.    The parent class is in error.
Q13SA
B
Q14
Fill in the blanks:
 
 
public interface Cat {     public void print(); }   class Dog ______ Cat {    public void print() { System.out.println("I"m a Cat and a ____"); } }   class Zebra ______ Cat {    public void print() { System.out.println("I'm a ___ and a ____"); } }
Q14A
implements Dog implements Cat Zebra
Q14S
Was the previous example a case of Overriding or Overloading?
Q14SA
Overriding
True or false?
When overloading...
 
 
The return types MUST be the same.
For a valid overload you must change more than the return type.
It is not possible to vary the access levels in any direction.
Q15
Q15A
F T F
Q15S
True or false?
When overriding...
 
 
Arguments can be different as long as return types are not compatible.
The access level may be different and the method can be less accessible.
Arguments and return types of the overriding method must be seen exactly like the overridden method in the super class.
Q15SA
F
F
T