java programming dr. randy kaplan. abstract classes and methods
TRANSCRIPT
![Page 1: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/1.jpg)
Java ProgrammingJava ProgrammingDr. Randy KaplanDr. Randy Kaplan
![Page 2: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/2.jpg)
Abstract Classes and MethodsAbstract Classes and Methods
![Page 3: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/24.jpg)
2424
ImplementationImplementation
![Page 25: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/25.jpg)
2525
ImplementationImplementation
![Page 26: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/26.jpg)
2626
ImplementationImplementation
![Page 27: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/27.jpg)
2727
ImplementationImplementation
![Page 28: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/28.jpg)
2828
ImplementationImplementation
![Page 29: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/29.jpg)
2929
ImplementationImplementation
![Page 30: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/30.jpg)
3030
ImplementationImplementation
![Page 31: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/31.jpg)
3131
ImplementationImplementation
![Page 32: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/32.jpg)
3232
ImplementationImplementation
![Page 33: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/33.jpg)
3333
ImplementationImplementation
![Page 34: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/34.jpg)
3434
ImplementationImplementation
![Page 35: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/35.jpg)
3535
ImplementationImplementation
![Page 36: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/36.jpg)
3636
ImplementationImplementation
![Page 37: Java Programming Dr. Randy Kaplan. Abstract Classes and Methods](https://reader035.vdocuments.mx/reader035/viewer/2022062518/56649f515503460f94c73e93/html5/thumbnails/37.jpg)
3737
ImplementationImplementation