java programming dr. randy kaplan. abstract classes and methods

37
Java Programming Dr. Randy Kaplan

Upload: fay-garrison

Post on 14-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

Java ProgrammingJava ProgrammingDr. Randy KaplanDr. Randy Kaplan

Page 2: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

Abstract Classes and MethodsAbstract Classes and Methods

Page 3: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

33

Abstract Classes and Methods

Abstract Classes and Methods

When a class is defined we usually do so with the idea that we will eventually instantiate the class

Another kind of class is not meant to be instantiated

This kind of class is called an abstract class

When a class is defined we usually do so with the idea that we will eventually instantiate the class

Another kind of class is not meant to be instantiated

This kind of class is called an abstract class

Page 4: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

44

Abstract Classes and Methods

Abstract Classes and Methods

Purpose

Provide an appropriate superclass from which other classes can inherit

These classes share a common design - for example common attributes

Purpose

Provide an appropriate superclass from which other classes can inherit

These classes share a common design - for example common attributes

Page 5: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

55

Abstract Classes and Methods

Abstract Classes and Methods

Classes that can be used to instantiate objects are called concrete classes

Abstract classes are too general to create real objects

Abstract classes only specify what is common among subclasses

We need to be more specific before we can create objects

Classes that can be used to instantiate objects are called concrete classes

Abstract classes are too general to create real objects

Abstract classes only specify what is common among subclasses

We need to be more specific before we can create objects

Page 6: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

66

Abstract Classes and Methods

Abstract Classes and Methods

Abstract classes sometimes constitute several levels of the hierarchy

In this hierarchy Shape is an abstract class

Abstract classes sometimes constitute several levels of the hierarchy

In this hierarchy Shape is an abstract class

ShapeShapeShapeShape

2DShape2DShape2DShape2DShape 3DShape3DShape3DShape3DShape

CircleCircleCircleCircle SquareSquareSquareSquare TriangleTriangleTriangleTriangle SphereSphereSphereSphere CubeCubeCubeCube TetrahedroTetrahedronn

TetrahedroTetrahedronn

Page 7: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

77

Abstract Classes and Methods

Abstract Classes and Methods

The classes 2DShape and 3DShape are also abstract classesThe classes 2DShape and 3DShape are also abstract classes

ShapeShapeShapeShape

2DShape2DShape2DShape2DShape 3DShape3DShape3DShape3DShape

CircleCircleCircleCircle SquareSquareSquareSquare TriangleTriangleTriangleTriangle SphereSphereSphereSphere CubeCubeCubeCube TetrahedroTetrahedronn

TetrahedroTetrahedronn

Page 8: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

88

Abstract Classes and Methods

Abstract Classes and Methods

An abstract class is made abstract by declaring it with the abstract keyword

An abstract class will contain one or more abstract methods

Example

public abstract void draw();

An abstract class is made abstract by declaring it with the abstract keyword

An abstract class will contain one or more abstract methods

Example

public abstract void draw();

Page 9: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

99

Abstract Classes and Methods

Abstract Classes and Methods

An abstract method does not provide an implementation

A class that contains any abstract methods must be declared as an abstract class even if that class contains some concrete methods

Constructors and static methods cannot be declared abstract

An abstract method does not provide an implementation

A class that contains any abstract methods must be declared as an abstract class even if that class contains some concrete methods

Constructors and static methods cannot be declared abstract

Page 10: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1010

Abstract Classes and Methods

Abstract Classes and Methods

Constructors are not inherited

therefore an abstract constructor could never be implemented

Static methods are inherited but,

they are not associated with particular objects

Constructors are not inherited

therefore an abstract constructor could never be implemented

Static methods are inherited but,

they are not associated with particular objects

Page 11: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1111

Abstract Classes and Methods

Abstract Classes and Methods

Abstract methods are meant to be overidden so they can process objects based on their types

It would not make sense to declare a static method as abstract

Abstract methods are meant to be overidden so they can process objects based on their types

It would not make sense to declare a static method as abstract

Page 12: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1212

Abstract Classes and Methods

Abstract Classes and Methods

Objects cannot be instantiated from abstract superclasses

Abstract superclasses CAN be used to declare variables that can hold references to objects of any concrete class derived from those abstract superclasses

Programs use these variables to manipulate subclass objects polymorphically

Objects cannot be instantiated from abstract superclasses

Abstract superclasses CAN be used to declare variables that can hold references to objects of any concrete class derived from those abstract superclasses

Programs use these variables to manipulate subclass objects polymorphically

Page 13: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1313

Abstract Classes and Methods

Abstract Classes and Methods

Static methods defined in abstract superclasses can also be used as any other static method can be used

Static methods defined in abstract superclasses can also be used as any other static method can be used

Page 14: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1414

Application of PolymorphismApplication of Polymorphism

Drawing program needs to display shapes including -

circles

trianglesd

rectangles

and those that the programmer adds to the system

Drawing program needs to display shapes including -

circles

trianglesd

rectangles

and those that the programmer adds to the system

Page 15: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1515

Application of PolymorphismApplication of Polymorphism

The drawing program uses Shape variables to manage the objects that are displayed

To draw an object -

drawing program uses superclass shape variable

superclass shape variable contains a reference to subclass object to invoke the objects draw method

The drawing program uses Shape variables to manage the objects that are displayed

To draw an object -

drawing program uses superclass shape variable

superclass shape variable contains a reference to subclass object to invoke the objects draw method

Page 16: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1616

Application of PolymorphismApplication of Polymorphism

This method - the one that is called on to draw the shape - is declared abstract in the superclass

Each concrete subclass MUST implement a method named draw to render the specific shape that is represented by that subclass

Each object in the shape inheritance knows how to draw itself

This method - the one that is called on to draw the shape - is declared abstract in the superclass

Each concrete subclass MUST implement a method named draw to render the specific shape that is represented by that subclass

Each object in the shape inheritance knows how to draw itself

Page 17: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1717

Using Abstract ClassesUsing Abstract Classes

In this example we will define a class inheritance hierarchy for a payroll system

Consider the following brief requirements specification

In this example we will define a class inheritance hierarchy for a payroll system

Consider the following brief requirements specification

Page 18: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1818

RequirementsRequirements

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 workers are paid by the hour and receive overtime pay for hours in excess of 40 hours, commission employyes are paid a perrcentage of their sales, and salaried commissioned employees are given a base salary plus a percentage of

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 workers are paid by the hour and receive overtime pay for hours in excess of 40 hours, commission employyes are paid a perrcentage of their sales, and salaried commissioned employees are given a base salary plus a percentage of

Page 19: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

1919

Requirements (cont.)Requirements (cont.)

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 implement a Java-based payroll application that implements calculations polymorphically.

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 implement a Java-based payroll application that implements calculations polymorphically.

Page 20: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2020

ImplementationImplementation

Abstract class Employee

The abstract class employee is used to represent the general concept of an employee

Abstract class Employee

The abstract class employee is used to represent the general concept of an employee

Page 21: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2121

ImplementationImplementation

The classes that extend Employee are -

SalariedEmployee

CommissionEmployee

HourlyEmployee

BasePlusCommissionEmployee will extend CommissionEmployee

The classes that extend Employee are -

SalariedEmployee

CommissionEmployee

HourlyEmployee

BasePlusCommissionEmployee will extend CommissionEmployee

Page 22: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2222

ImplementationImplementation

The Employee class declcares the interface to the hierarchy

The interface to the hierarchy consists of the set of methods that a program can invoke on all Employee objects

Here the term interface is used in a general sense

The Employee class declcares the interface to the hierarchy

The interface to the hierarchy consists of the set of methods that a program can invoke on all Employee objects

Here the term interface is used in a general sense

Page 23: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2323

ImplementationImplementation

Elements of the abstract class Employee

first name

last name

social security number

Elements of the abstract class Employee

first name

last name

social security number

Page 24: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2424

ImplementationImplementation

Page 25: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2525

ImplementationImplementation

Page 26: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2626

ImplementationImplementation

Page 27: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2727

ImplementationImplementation

Page 28: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2828

ImplementationImplementation

Page 29: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

2929

ImplementationImplementation

Page 30: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3030

ImplementationImplementation

Page 31: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3131

ImplementationImplementation

Page 32: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3232

ImplementationImplementation

Page 33: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3333

ImplementationImplementation

Page 34: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3434

ImplementationImplementation

Page 35: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3535

ImplementationImplementation

Page 36: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3636

ImplementationImplementation

Page 37: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods

3737

ImplementationImplementation