As a programming language, Java's object-oriented nature is key to creating powerful, reusable code and applications that are easy to maintain and extend. That being said, many people learn Java syntax without truly understanding its object-oriented roots, setting them up to fail to harness all of the power of Java. This book is your key to learning both!
This new third edition of Beginning Java Objects: From Concepts to Code discusses Java syntax, object principles, and how to properly structure the requirements of an application around an object architecture. It is unique in that it uses a single case study of a Student Registration System throughout the book, carrying the reader from object concepts, to object modeling, to building actual code for a full-blown application. A new chapter covers a technology-neutral discussion of the principles of building a three-tier architecture using Java, introducing the notion of model layer – presentation layer – data layer separation.
Coding examples used throughout the book are Java version-neutral. The core object-oriented principles that you will learn from this book are timeless, and are relevant to all versions of the Java language, as well as to many other object-oriented languages.
The book can be used for individual self-study or as a university-level textbook.
What You Will Learn
- Know basic object-oriented principles, from the simplest notion of classes and objects through the power of encapsulation, abstract classes, and polymorphism
- Approach the requirements for an application to structure it properly around objects
- Render the resultant object model into Java code, building a complete functioning model layer for the Student Registration System case study
- Conceptually round out an object layer by adding presentation and data layers
Author(s): Jacquie Barker
Edition: 3
Publisher: Apress
Year: 2023
Language: English
Commentary: TOC's links are broken.
Pages: 860
City: Berkeley, CA
Tags: Java; Java EE; Object-Oriented Programming; Unified Modeling Language; UML; Modeling; Object; Reusable Code
About the Author
About the Technical Reviewer
Preface
Introduction
Student Registration System (SRS) Case Study
Chapter 1: Abstraction and Modeling
Simplification Through Abstraction
Generalization Through Abstraction
Organizing Abstractions into Classification Hierarchies
Abstraction as the Basis for Software Development
Reuse of Abstractions
Inherent Challenges
What Does It Take to Be a Successful Object Modeler?
Summary
Chapter 2: Some Java Basics
Java Is Architecture Neutral
Anatomy of a Simple Java Program
Comments
Traditional Comments
End-of-Line Comments
Java Documentation Comments
The Class Declaration
The main Method
Setting Up a Simple Java Development Environment
The Mechanics of Java
Compiling Java Source Code into Bytecode
Executing Bytecode
Primitive Types
Variables
Variable Naming Conventions
Variable Initialization
The String Type
Case Sensitivity
Java Expressions
Arithmetic Operators
Relational and Logical Operators
Evaluating Expressions and Operator Precedence
The Type of an Expression
Automatic Type Conversions and Explicit Casting
Loops and Other Flow Control Structures
if Statements
switch Statements
for Statements
while Statements
Jump Statements
Block-Structured Languages and the Scope of a Variable
Printing to the Console Window
print vs. println
Escape Sequences
Elements of Java Style
Proper Use of Indentation
Use Comments Wisely
Placement of Braces
Descriptive Variable Names
Summary
Chapter 3: Objects and Classes
Software at Its Simplest
Functional Decomposition
The Object-Oriented Approach
What Is an Object?
State/Data/Attributes
Behavior/Operations/Methods
What Is a Class?
A Note Regarding Naming Conventions
Declaring a Class, Java Style
Instantiation
Encapsulation
User-Defined Types and Reference Variables
Naming Conventions for Reference Variables
Instantiating Objects: A Closer Look
Garbage Collection
Objects As Attributes
A Compilation “Trick”: “Stubbing Out” Classes
Composition
The Advantages of References As Attributes
Three Distinguishing Features of an Object-Oriented Programming Language
Summary
Chapter 4: Object Interactions
Events Drive Object Collaboration
Declaring Methods
Method Headers
Method Naming Conventions
Passing Arguments to Methods
Method Return Types
An Analogy
Method Bodies
Features May Be Declared in Any Order
return Statements
Methods Implement Business Rules
Objects As the Context for Method Invocation
Java Expressions, Revisited
Capturing the Value Returned by a Method
Method Signatures
Choosing Descriptive Method Names
Method Overloading
Message Passing Between Objects
Delegation
Obtaining Handles on Objects
Objects As Clients and Suppliers
Information Hiding/Accessibility
Public Accessibility
Private Accessibility
Publicizing Services
Method Headers, Revisited
Accessing the Features of a Class from Within Its Own Methods
Accessing Private Features from Client Code
Declaring Accessor Methods
Recommended “Get”/“Set” Method Headers
IDE-Generated Get/Set Methods
The “Persistence” of Attribute Values
Using Accessor Methods from Client Code
The Power of Encapsulation Plus Information Hiding
Preventing Unauthorized Access to Encapsulated Data
Helping Ensure Data Integrity
Limiting “Ripple Effects” When Private Features Change
Using Accessor Methods from Within a Class’s Own Methods
Exceptions to the Public/Private Rule
Constructors
Default Constructors
Writing Our Own Explicit Constructors
Passing Arguments to Constructors
Replacing the Default Parameterless Constructor
More Elaborate Constructors
Overloading Constructors
An Important Caveat Regarding the Default Constructor
Using the “this” Keyword to Facilitate Constructor Reuse
Software at Its Simplest, Revisited
Summary
Chapter 5: Relationships Between Objects
Associations and Links
Multiplicity
One-to-One (1:1)
One-to-Many (1:m)
Many-to-Many (m:m)
Multiplicity and Links
Aggregation and Composition
Inheritance
Responding to Shifting Requirements with a New Abstraction
(Inappropriate) Approach #1: Modify the Student Class
(Inappropriate) Approach #2: “Clone” the Student Class to Create a GraduateStudent Class
The Proper Approach (#3): Taking Advantage of Inheritance
The “is a” Nature of Inheritance
The Benefits of Inheritance
Class Hierarchies
The Object Class
Is Inheritance Really a Relationship?
Avoiding “Ripple Effects” in a Class Hierarchy
Rules for Deriving Classes: The “Do’s”
Overriding
Reusing Superclass Behaviors: The “super” Keyword
Rules for Deriving Classes: The “Don’ts”
Private Features and Inheritance
Inheritance and Constructors
Constructors Are Not Inherited
super(...) for Constructor Reuse
Replacing the Default Parameterless Constructor
A Few Words About Multiple Inheritance
Three Distinguishing Features of an OOPL, Revisited
Summary
Chapter 6: Collections of Objects
What Are Collections?
Collections Are Defined by Classes and Must Be Instantiated
Collections Organize References to Other Objects
Collections Are Encapsulated
Three Generic Types of Collection
Ordered Lists
Dictionaries
Sets
Arrays As Simple Collections
Declaring and Instantiating Arrays
Accessing Individual Array Elements
Initializing Array Contents
Manipulating Arrays of Objects
A More Sophisticated Type of Collection: The ArrayList Class
Using the ArrayList Class: An Example
Import Directives and Packages
The Namespace of a Class
User-Defined Packages and the Default Package
Generics
ArrayList Features
Iterating Through ArrayLists
Copying the Contents of an ArrayList into an Array
The HashMap Collection Class
The TreeMap Class
The Same Object Can Be Simultaneously Referenced by Multiple Collections
Inventing Our Own Collection Types
Approach #1: Designing a New Collection Class from Scratch
Approach #2: Extending a Predefined Collection Class (MyIntCollection)
Wrapper Classes for Primitive Types
Reusing a Base Class Constructor
Overriding the add Method
Putting MyIntCollection to Work
Approach #3: Encapsulating a Standard Collection (MyIntCollection2)
Putting MyIntCollection2 to Work
Trade-Offs of Approach #2 vs. Approach #3
Collections As Method Return Types
Collections of Derived Types
Revisiting Our Student Class Design
The courseLoad Attribute of Student
The transcript Attribute of Student
The transcript Attribute, Take 2
Our Completed Student Data Structure
Summary
Chapter 7: Some Final Object Concepts
Polymorphism
Polymorphism Simplifies Code Maintenance
Three Distinguishing Features of an Object-Oriented Programming Language
The Benefits of User-Defined Types
The Benefits of Inheritance
The Benefits of Polymorphism
Abstract Classes
Implementing Abstract Methods
Abstract Classes and Instantiation
Declaring Reference Variables of Abstract Types
An Interesting Twist on Polymorphism
Interfaces
Implementing Interfaces
Another Form of the “Is A” Relationship
Interfaces and Casting
Implementing Multiple Interfaces
Interfaces and Casting, Revisited
Interfaces and Instantiation
Interfaces and Polymorphism
The Importance of Interfaces
Example #1
Example #2
Static Features
Static Variables
A Design Improvement: Burying Implementation Details
Static Methods
Restrictions on Static Methods
Utility Classes
The final Keyword
Public Static Final Variables and Interfaces
Custom Utility Classes
Summary
Chapter 8: The Object Modeling Process in a Nutshell
The “Big Picture” Goal of Object Modeling
Modeling Methodology = Process + Notation + Tool
My Recommended Object Modeling Process, in a Nutshell
Thoughts Regarding Object Modeling Software Tools
Advantages of Using CASE Tools
Ease of Use
Added Information Content
Automated Code Generation
Project Management Aids
Some Drawbacks of Using CASE Tools
A Reminder
Summary
Chapter 9: Formalizing Requirements Through Use Cases
What Are Use Cases?
Functional vs. Technical Requirements
Involving the Users
Actors
Identifying Actors and Determining Their Roles
Diagramming a System and Its Actors
Specifying Use Cases
Matching Up Use Cases with Actors
To Diagram or Not to Diagram?
Summary
Chapter 10: Modeling the Static/Data Aspects of the System
Identifying Appropriate Classes
Noun Phrase Analysis
Refining the Candidate Class List
Revisiting the Use Cases
Producing a Data Dictionary
Determining Associations Between Classes
Association Matrices
Identifying Attributes
UML Notation: Modeling the Static Aspects of an Abstraction
Classes, Attributes, and Operations
Relationships Between Classes
Associations
Aggregation
Inheritance
Reflecting Multiplicity
Object/Instance Diagrams
Associations As Attributes
Information “Flows” Along an Association “Pipeline”
“Mixing and Matching” Relationship Notations
Association Classes
Our “Completed” Student Registration System Class Diagram
Metadata
Summary
Chapter 11: Modeling the Dynamic/Behavioral Aspects of the System
How Behavior Affects State
Events
An Object May Change Its State
An Object May Direct an Event (Message) Toward Another Object
An Object May Return a Value
An Object May Interact with the External Boundaries of Its System
An Object May Seemingly Ignore an Event
Scenarios
Scenario #1 for the “Register for a Course” Use Case
Scenario #2 for the “Register for a Course” Use Case
Sequence Diagrams
Determining Objects and External Actors for Scenario #1
Preparing the Sequence Diagram
Using Sequence Diagrams to Determine Methods
Communication Diagrams
Revised SRS Class Diagram
Summary
Chapter 12: Wrapping Up Our Modeling Efforts
Testing the Model
Revisiting Requirements
Reusing Models: A Word About Design Patterns
Summary
Chapter 13: A Few More Java Details
Java-Specific Terminology
Java Archive (jar) Files
Creating Jar Files
Inspecting the Contents of a Jar File
Using the Bytecode Contained Within a Jar File
Extracting Contents from Jar Files
“Jarring” Entire Directory Hierarchies
Javadoc Comments
The Object Nature of Strings
Operations on Strings
Strings Are Immutable
The StringBuffer Class
The StringTokenizer Class
Instantiating Strings and the String Literal Pool
Testing the Equality of Strings
Message Chains
Object Self-Referencing with “this”
Java Exception Handling
The Mechanics of Exception Handling
The try Block
The catch Block
The finally Block
Catching Exceptions
Interpreting Exception Stack Traces
The Exception Class Hierarchy
Catching the Generic Exception Type
Compiler Enforcement of Exception Handling
Taking Advantage of the Exception That We’ve Caught
Nesting of Try/Catch Blocks
User-Defined Exception Types
Throwing Multiple Types of Exception
Enum(eration)s
Providing Input to Command-Line-Driven Programs
Accepting Command-Line Arguments: The args Array
Introducing Custom Command-Line Flags to Control a Program’s Behavior
Accepting Keyboard Input: The Scanner Class
Using Wrapper Classes for Input Conversion
Features of the Object Class
Determining the Class That an Object Belongs To
Testing the Equality of Objects
Overriding the equals Method
Overriding the toString Method
Static Initializers
Variable Initialization, Revisited
Variable Arguments (varargs)
Summary
Chapter 14: Transforming the Model into Java Code
Suggestions for Getting the Maximum Value from This Chapter
The SRS Class Diagram Revisited
The Person Class (Specifying Abstract Classes)
Attributes of Person
Person Constructors
Person Accessor Methods
toString()
display()
The Student Class (Reuse Through Inheritance, Extending Abstract Classes, Delegation)
Attributes of Student
Student Constructors
Student Accessor Methods
display()
toString()
displayCourseSchedule()
addSection()
dropSection()
isEnrolledIn()
isCurrentlyEnrolledInSimilar()
getEnrolledSections()
printTranscript()
The Professor Class (Bidirectionality of Relationships)
Professor Attributes
agreeToTeach()
displayTeachingAssignments()
The Course Class (Reflexive Relationships, Unidirectional Relationships)
Course Attributes
Course Methods
hasPrerequisites()
getPrerequisites()
scheduleSection()
The Section Class (Representing Association Classes, Public Static Final Attributes, Enums)
Section Attributes
The Use of an Enum(eration) Type
enroll()
drop()
postGrade()
getGrade()
confirmSeatAvailability()
Delegation Revisited
The ScheduleOfClasses Class
ScheduleOfClasses Attributes
addSection()
findSection()
isEmpty()
The TranscriptEntry Association Class (Static Methods)
TranscriptEntry Attributes
TranscriptEntry Constructor
validateGrade(), passingGrade()
The Transcript Class
Transcript Attributes
verifyCompletion()
The SRS Driver Program
Public Static Attributes
The main Method
Summary
Chapter 15: Building a Three-Tier User Driven Application
A Three-Tier Architecture
What Does the Controller Do?
Building a Persistence/Data Tier
Building a Web-Based Presentation Layer
Example Controller Logic
The Importance of Model–Data Layer–View Separation
Summary
Further Reading
Appendix A: Alternative Case Studies
Case Study #1: Prescription Tracking System
Background
Simplifying Assumptions
Case Study #2: Conference Room Reservation System
Background
Goals for the System
Case Study #3: Blue Skies Airline Reservation System
Background
Other Simplifying Assumptions
Index