Lab11 AbstractInterface
Lab11 AbstractInterface
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
Abstract and Interfaces
Lab Exercise
Abstract classes and interfaces extend the power of polymorphism in Java. They
also prevent the construction of nonsensical objects and make sure classes adhere
to expected functionality. In this lab, you continue to explore polymorphism in Java.
Scenario
Currently, the Good class allows instances of a Good to be created. In actuality, this
class serves as a template for the subclasses (Solid and Liquid), so instances of a
Good should not be created. For instance, consider the volume method on Good.
What is the volume of such a nebulous object?
1.1 Make Good abstract. In the Package Explorer view, double-click on the
Good.java file in the com.acme.domain package to open the file in a Java
editor. Use the “abstract” key word to make this class abstract.
1.2 Make the volume( ) method abstract in Good. Remove the current
implementation of the volume( ) method in Good, and replace it with an
abstract definition (see below). Making this method abstract forces the
Solid and Liquid classes to implement and override volume( ).
1.3 Save Good.java and run the TestOrders.java file to ensure your application
still works the same way.
1.4.1 In the main method of TestOrders.java, try to create an instance of the now
abstract Good.
1.4.2 This will also require you import the Good class.
import com.acme.domain.Good;
1.4.3 You should get a compiler error that prevents an instance of Good from
being created.
In addition to its goods, like any large conglomerate, Acme also offers a number of
services, such as Road Runner Eradication and Rocket Sled Assembly. An order can
then be placed for a good or a service. In this step, you create a new Service class
and an interface that both Goods and Services implement.
2.1 Create the Service Class. In the Package Explorer view, right-click on the
com.acme.domain package in the AcmeOrderSystem project, and select New
> Class.
2.2 In the New Java Class window, enter Service as the new class name, and
click the Finish button.
The Service class should not extend any class other than java.lang.Object.
2.6 Add a toString( ) method to Service. The toString( ) method for a Service
should display the name and estimated duration for a Service.
2.7 Save the Service.java file once you have finished coding, and ensure there
are no compiler errors.
3.1 Create the Product interface. In the Package Explorer view, right-click on
the com.acme.domain package in the AcmeOrderSystem project, and select
New > Interface.
3.2 In the New Java Interface window, enter Product as the new interface name,
and click the Finish button.
3.3 Add the abstract methods. In the Product.java editor view, add the three
common methods shared by all types of products.
3.3.1 All products (either goods or services) have a product name. Therefore,
create getName( ) and setName( ) abstract methods in the new interface.
3.3.2 All products also have a toString( ) method. Therefore, create the toString( )
abstract method in the new interface.
3.4 Save the Product.java interface, and make sure there are no compiler
errors.
3.5 Modify Good and Service to implement the new Product interface.
3.5.1 Open the Good.java and Service.java files in editor views.
3.5.2 Use the “implements” key word on both of these classes to signal to the
compiler that all Good and Service must provide the getName( ), setName( ), and
toString( ) methods. The change to Good is shown below. Do the same work in
Service.
Note: Why do Solid and Liquid not have to use the “implements” keyword in their
class definitions? These classes also must implement the Product interface, as they
extend Good, which implements Product.
3.5.3 Since these classes already provide the three abstract methods of Product,
no further work is required. Save Product, Good, and Service, and make sure there
are no compiler errors.
Through the power of polymorphism, orders currently allow any type of Good to be
created and associated with an order through the product field. However, an order
may be for a service as well. As a Service is not a type of Good, the Order class must
be modified to allow for any Product type.
4.1 Change the product field’s type. In Order.java, change the type of product
from Good to Product.
4.2 Update the getter/setter. Modify the getter and setter method for product
in the Order class to get and set a Product, rather than a Good. The code for
the new getter method is shown below.
4.3 Change the Order constructor to use a Product, rather than a Good, when
creating an Order.
4.4 Save your changes to Order, and then test the new Order class that uses the
Product interface. Run TestOrders and check the output. The output
should not change.
Because Services share the same interface (Product) as Goods, you should be able to
create Orders with Service objects as the “product” without requiring other changes.
import com.acme.domain.Service;
5.2 Create an Order with a service. In TestOrders, add the following lines of
code at the end of the main( ) method.
5.3 Save the file, and make sure there are no compiler errors in your code. Run
TestOrders, and make sure that an Order associated with a Service works
the same as an Order associated with a Good.
The total bill for: 125 ea. Acme Balloon-1401 that is 375.0
CUBIC_FEET in size for Bugs Bunny is 1040.0
The total bill for: 1 ea. Road Runner Eradication(a 14 day
service) for Daffy Duck is 20000.0
Because both Service and Good objects share the same interface, they can
be used interchangeably (to some extent) with Orders. This is
polymorphism through interfaces!
Lab Solutions
Service.java
package com.acme.domain;
Product.java
package com.acme.domain;
Good.java
package com.acme.domain;
Order.java
package com.acme.domain;
import com.acme.utils.MyDate;
this.product = product;
}
* Order.taxRate);
return orderAmount * Order.taxRate;
}
TestOrders.java
package com.acme.testing;
import com.acme.domain.Order;
import com.acme.domain.Service;
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());