cs111: programming language ii - · pdf filecase study: payroll system 8 a company pays its...

25
CS111: PROGRAMMING LANGUAGE II Lecture 8(a): Abstract Classes Computer Science Department 1

Upload: dokien

Post on 26-Mar-2018

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

CS111: PROGRAMMING

LANGUAGE II

Lecture 8(a): Abstract Classes Computer Science

Department

1

Page 2: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Lecture Contents

Dr. Amal Khalifa, 2014

Abstract base classes

Concrete classes

2

Page 3: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Abstract Classes and Methods

Dr. Amal Khalifa, 2014

CommunityMember is not supposed to be used to create objects

abstract superclass

Page 4: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

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.

Page 5: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

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.

Page 6: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

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.

Page 7: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Working with abstract base classes

Programming Pitfalls!! 7

dr. Amal Khalifa, 2014

Page 8: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

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.

Page 9: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Abstract class names are italicized in the UML.

9

dr. Amal Khalifa, 2014

Abstract class names are italicized in the UML.

Page 10: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

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

Page 11: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Concrete Subclasses

11

Page 12: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

The Employee class

Abstract class

abstract

keyword

12

dr. Amal Khalifa, 2014

Page 13: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

dr. Amal Khalifa, 2014 13

Page 14: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

dr. Amal Khalifa, 2014 14

Page 15: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Concrete Subclasses

15

Page 16: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

SalariedEmployee class

One concrete

class

16

dr. Amal Khalifa, 2014

Page 17: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

dr. Amal Khalifa, 2014 17

Page 18: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

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,

Page 19: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

dr. Amal Khalifa, 2014 19

Page 20: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

dr. Amal Khalifa, 2014 20

Page 21: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

CommissionEmployee class 21

commission

employees

are paid a

percentage

of their

sales

Page 22: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

22 dr. Amal Khalifa, 2014

Page 23: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

CommissionEmployee class

dr. Amal Khalifa, 2014

23

The last one!!

Page 24: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

dr. Amal Khalifa, 2014 24

Page 25: CS111: PROGRAMMING LANGUAGE II - · PDF fileCase Study: Payroll System 8 A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid

Working on Chapter 10…..

That’s all for today…..

dr. Amal Khalifa, 2014

25