Pylymorphism
Pylymorphism
About Intertech
Thank you for choosing Intertech for your training from Udemy. The next page of this document
will get you started with your lab if you’d like to skip ahead. Below is a brief overview of
Intertech as well as a promo code for you for future live Intertech trainings.
Our training organization offers live in-classroom and online deliveries, private on-site deliveries,
and on-demand options such as the course in which you’ve enrolled from Udemy. We cover a
broad spectrum of .NET, Java, Agile/Scrum, Web Development, and Mobile technologies. See
more information on our training and search for courses by clicking here.
As a Udemy customer, you can save on any live in-classroom, live online, or onsite training with
Intertech as well. Just use promo code “Udemy_Labs” when enrolling and save 35% on the
course price.
Page 1
Polymorphism
Lab Exercise
Polymorphism
Optionally explore the cast operation to cast a general reference back to a more
specific type of object.
Scenario
Given the product hierarchy (Good, Solid, Liquid), you now have a fine set of
“product” objects to associate with an order (replacing the current String for
product). Of course, an order could be for a Solid or a Liquid type, so what type
should be used on the product field of Order? Here is where polymorphism can help
out.
Currently, the product field on Order is of type String. Now it is time to associate the
Good type objects to orders.
1.1 Change the product field’s type. In the Package Explorer view, double-click
on the Order.java file in the com.acme.domain package to open the file in a
Java editor.
Note: This will cause a compiler error on the getter and setter for product, as well as
the Order’s constructor.
1.2 Update the getter/setter for product. Modify the getter and setter method
for product in the Order class to get and set a Good, rather than a String.
The code for the new getter method is shown below.
1.3 Update the Order constructor to create a new order with a Good object,
instead of a String.
1.4 Update TestOrders.java to create and use an instance of a Good for the
product field of an Order, rather than a String. Replace the first few lines of
the main( ) method shown below…
Note: This is polymorphism at work. Notice how on Order the type specified was a
Good. Here you create a Solid for use in the product field! You could have also
created a Liquid object and passed it in as the product for the order. Polymorphism
allows any specific object (Solid or Liquid) to be used in a general reference (Good in
this case).
1.6 Test the new Order class with associated Good products by running
TestOrders. The output should change a little. Why does it? How does the
display of product information differ, and what is occurring to make this
happen?
Given two MyDate objects, do they represent the same date? In other words, are
they equal? Given the current MyDate definition, you might be surprised by the
result. In this step, you observe the default implementation of the equals( ) method
and then override the java.lang.Object equals( ) method to provide a more
appropriate response among MyDate objects.
2.1.2 Following the creation of these objects, use the equals( ) method to
determine whether the two are equal.
if (newYear.equals(fiscalStart))
System.out.println("These two dates are equal");
else
System.out.println("These two dates are not equal");
2.1.3 Save TestMyDate and run the TestMyDate.java file. The results for this
operation should indicate the two objects are not equal! What does this tell you
about the equals( ) method?
...
These two dates are not equal
Add a new equals( ) method that overrides the java.lang.Object equals( ) method.
Note: Did you see the use of the instanceof operator in this method? Why should you
use this operator in the equal method?
2.2.2 Save the MyDate.java file, and rerun the TestMyDate.java file. Now the two
dates should be equal.
Try creating two MyDate objects that do not have the same day, month, and
year field values, and then check to see if they are equal.
Lab Solutions
Order.java
package com.acme.domain;
import com.acme.utils.MyDate;
TestOrders.java
package com.acme.testing;
import com.acme.domain.Order;
import com.acme.domain.Solid;
import com.acme.domain.Good.UnitOfMeasureType;
import com.acme.utils.MyDate;
System.out.println(anvil);
System.out.println(balloons);
Order.setTaxRate(0.06);
System.out.println("The tax Rate is currently: " +
Order.taxRate);
Order.computeTaxOn(3000.00);
anvil.computeTax();
balloons.computeTax();
System.out.println("The total bill for: " + anvil + " is "
+ anvil.computeTotal());
System.out.println("The total bill for: " + balloons + " is
"
+ balloons.computeTotal());
}
}
TestMyDate.java
package com.acme.testing;
import com.acme.utils.MyDate;
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
MyDate.leapYears();
MyDate.java
package com.acme.utils;
// Constructors:
// 1. Same name as the class
// 2. No return type
// Methods
public String toString() {
return month + "/" + day + "/" + year;
}
return true;
}
}
return false;
}
}
Bonus Lab
3.1 At the bottom of the main( ) method in TestOrders.java, attempt to get and
print out the volume and length of the Good associated with the anvil order.
Calling on the volume method on the Order’s product works, but you get a
compiler error when trying to get the length.
3.2 Fix this by using casting to cast the product reference back to its real Solid
type and then calling the getLength( ) method.
3.3 What would happen, however, if the product was really a Liquid and not a
Solid?