Practical 10: Abstract Classes and Interfaces
If you look into the hierarchy of classes in Java, you may find that you are not able to create objects out of certain superclasses. This is often because having objects based on the structure of such superclasses don't make sense. In such cases like these, we often keep these classes (and sometimes some of their methods as well) as abstract.
Activity: Implementing an Abstract Class and Abstract Methods
Looking back at the program we just went through for the past two weeks, it is not very useful to have objects of just the Employee
class.
We can change it into an abstract class by adding the abstract
keyword in the class declaration like as follows:
Here, we also modified the constructor declaration to use the protected
keyword instead of the public
keyword.
It barely makes a difference for the time being, but it does ensure that it cannot be invoked unless from a linked subclass.
Note that in this class, the calculateSalary()
method is also not useful.
Apart from creating abstract classes, the abstract
keyword can be used to create abstract methods as and when necessary.
Abstract methods can still be overridden by subclasses.
However, the visibility modifier of such declarations cannot be more private than that implemented in the superclass.
In this case, you are not allowed to override the calculateSalary()
method with any other visibility modifier apart from public
.
However, if the declaration here in the superclass is protected
for example, subclasses can use the same visibility modifier or use a less private one like public
.
Implementing a Simple Interface
Let's create an interface class named Manners
.
We will only implement it in the AcademicStaff
class, but the methods introduced from the Manners
interface will be reflected on the related subclasses.
This interface will have one method called introduce()
, but there is no restriction as to how many methods are introduced here.
In order to introduce the use of this interface in the AcademicStaff
class, introduce it using the implements
keyword like as follows:
Note
The usage of the implements
keyword can be used alongside the extends
keyword, should an interface ever need to be implemented on a subclass.
When implementing an interface, one would require the method to be implemented.
In the AcademicStaff
class, assume that the introduce()
prints out a greeting statement.
AcademicStaff.java | |
---|---|
Optional Activity
How would you implement this interface in the subclasses should you want the introduce()
method to act differently based on which subclass it is being called from?
Task
In this practical task, you will build simple programs using abstract classes. You will also learn how to add polymorphic behavior to the program using abstract methods.
Product.java
The skeleton code for Product.java
is given below.
The Product
class is an abstract class that has an abstract method called computeSalePrice()
.
Electronics.java
The Electronics
class itself is an abstract class because it does not provide implementation of the computeSalePrice()
abstract method.
-
Write
MP3Player.java
. TheMP3Player
class extends theElectronics
class. TheMP3Player
class should implement thecomputeSalePrice()
method with the following statement:Note: In addition to the implementation of the abstract method, you also need to include a constructor and a
String
instance variable calledcolor
. -
Write
TV.java
. TheTV
class extends theElectronics
abstract class. TheTV
class should also implement thecomputeSalePrice()
method, but with the following statement:Note: In addition to the implementation of the abstract method, you also need to include a constructor and an
int
instance variable calledquantity
. -
Complete
Book.java
. TheBook
class extends theProduct
class. TheBook
class should also implement thecomputeSalePrice()
method, but with the following statement:You may continue with the given code for
Book.java
as follows: -
Run
Main.java
.You should observe the following output results:
Item number 0: Type = myonlineshop.TV, Regular price = 1000.0, Sale price = 800.0 Item number 1: Type = myonlineshop.TV, Regular price = 2000.0, Sale price = 1600.0 Item number 2: Type = myonlineshop.MP3Player, Regular price = 250.0, Sale price = 225.0 Item number 3: Type = myonlineshop.Book, Regular price = 34.0, Sale price = 17.0 Item number 4: Type = myonlineshop.Book, Regular price = 15.0, Sale price = 7.5 totalRegularPrice = 3299.0 totalSalePrice = 2649.5