java programming dr. randy kaplan. abstract classes and methods

Post on 14-Jan-2016

221 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java ProgrammingJava ProgrammingDr. Randy KaplanDr. Randy Kaplan

Abstract Classes and MethodsAbstract 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

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

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

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

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

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();

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

2424

ImplementationImplementation

2525

ImplementationImplementation

2626

ImplementationImplementation

2727

ImplementationImplementation

2828

ImplementationImplementation

2929

ImplementationImplementation

3030

ImplementationImplementation

3131

ImplementationImplementation

3232

ImplementationImplementation

3333

ImplementationImplementation

3434

ImplementationImplementation

3535

ImplementationImplementation

3636

ImplementationImplementation

3737

ImplementationImplementation

top related