cs111: programming language ii - · pdf filecase study: payroll system 8 a company pays its...
TRANSCRIPT
CS111: PROGRAMMING
LANGUAGE II
Lecture 8(a): Abstract Classes Computer Science
Department
1
Lecture Contents
Dr. Amal Khalifa, 2014
Abstract base classes
Concrete classes
2
Abstract Classes and Methods
Dr. Amal Khalifa, 2014
CommunityMember is not supposed to be used to create objects
abstract superclass
Abstract Classes and Methods
dr. Amal Khalifa, 2014
4
Abstract classes
Sometimes it’s useful to declare classes for which you never intend to create objects.
Cannot be used to instantiate objects—abstract classes are incomplete.
An abstract class provides a superclass from which other classes can inherit and thus share a common design.
Used only as superclasses in inheritance hierarchies, so they are sometimes called abstract superclasses.
Not all hierarchies contain abstract classes.
Abstract classes sometimes constitute several levels of a hierarchy.
Concrete Classes
dr. Amal Khalifa, 2014
5
Classes that can be used to instantiate objects are called concrete classes.
must declare the “missing pieces” in their abstract superclasses
provide implementations of every method they declare (some of the implementations can be inherited).
provide the specifics that make it reasonable to instantiate objects.
Abstract Classes and Methods
dr. Amal Khalifa, 2014
6
Declared with the keyword abstract.
normally contains one or more abstract methods. Declared with keyword abstract
public abstract void draw(); // abstract method
Abstract methods do not provide implementations.
A class that contains abstract methods must be an abstract class even if that class contains some concrete (nonabstract) methods.
Each concrete subclass of an abstract superclass also must provide concrete implementations of each of the superclass’s abstract methods.
Constructors and static methods cannot be declared abstract.
Working with abstract base classes
Programming Pitfalls!! 7
dr. Amal Khalifa, 2014
Case Study: Payroll System 8
A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and base-salaried commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries. The company wants to write a Java application that performs its payroll calculations polymorphically.
Abstract class names are italicized in the UML.
9
dr. Amal Khalifa, 2014
Abstract class names are italicized in the UML.
Abstract Superclass Employee
Each Employee has:
first name,
last name, and
a social security number
Class Employee provides :
toString method
the get and set methods for instance variables.
An earnings method
An abstract method—there is not enough information to determine what amount earnings should return.
applies to all employees,
each earnings calculation depends on the employee’s class.
Each subclass overrides earnings with an appropriate implementation.
10
dr. Amal Khalifa, 2014
Concrete Subclasses
11
The Employee class
Abstract class
abstract
keyword
12
dr. Amal Khalifa, 2014
dr. Amal Khalifa, 2014 13
dr. Amal Khalifa, 2014 14
Concrete Subclasses
15
SalariedEmployee class
One concrete
class
16
dr. Amal Khalifa, 2014
dr. Amal Khalifa, 2014 17
HourlyEmployee class
dr. Amal Khalifa, 2014
18
hourly
employees
are paid by
the hour and
receive
overtime pay
(i.e., 1.5 times
their hourly
salary rate)
for all hours
worked in
excess of 40
hours,
dr. Amal Khalifa, 2014 19
dr. Amal Khalifa, 2014 20
CommissionEmployee class 21
commission
employees
are paid a
percentage
of their
sales
22 dr. Amal Khalifa, 2014
CommissionEmployee class
dr. Amal Khalifa, 2014
23
The last one!!
dr. Amal Khalifa, 2014 24
Working on Chapter 10…..
That’s all for today…..
dr. Amal Khalifa, 2014
25