Additional class features in java object-oriented programming, including the this reference, memberwise assignment, class inheritance, and reference variables as class members. It also covers common programming errors and provides examples of single-class and multiclass gas pump simulations. Students will gain a deeper understanding of java class capabilities and applications.
Download Java OOP: Class-Centered Approach - Chapter 11: Extra Class Capabilities and more Slides Applications of Computer Sciences in PDF only on Docsity!
Chapter 11:
Additional Class
Capabilities
Object-Oriented Program
Development Using Java: A
Class-Centered Approach
Objectives
Additional Class Features
Additional Applications
Class Inheritance
Reference Variables as Class Members
Common Programming Errors
The this Reference
Each object maintains its own set of instance
variables
Permits each object to have its own clearly defined state
When an object is created a distinct area of
memory is set aside for instance variables
The this Reference (continued)
Member methods
Unlike data storage
Only one copy of each member method is maintained in memory per class
Requires providing a means of identifying which specific object a member method should be operating on
Use the this reference to specify the current instance
Memberwise Assignment
(continued)
Cascading method call:
Example:
a.setEqual(b.setEqual(c))
Method must return same data type as parameter
Can use the this reference in return statement
Additional Applications
Complete simulation:
Using two separate classes:
Gas pump
Customer
Application 1: A Single-Class
Gas Pump Simulation
(continued)
Attributes of interest for pump:
Amount of gallons in the supply tank
Price per gallon
Required operations include:
Supplying the initial values for the pump’s attributes
Interrogating the pump for attribute values
Satisfying the request for gas
Application 2: A Multiclass
Gas Pump Simulation
(continued)
Each interaction between Customer and Pump:
Obtain Customer arrival time
Obtain Customer request for gas
Activate Pump with request
Inheritance
Capability of deriving one class from another class
Base class:
Original class
Also called:
Parent
Superclass
Inheritance (continued)
Simple inheritance
Each derived type has only one immediate base type
Multiple inheritance
Derived type has two or more base types
Not supported in Java
Class hierarchies
Illustrate the order in which one class is derived from another