Chapter 1: Intro.
to OO
- Structured Design
Divide a problem into smaller subproblems
Aka. Top-down design / Stepwise design / Modular programming
- OOD’s 3 Basic Principles
Encapsulation
Ability to combine data & operations in a single unit
Inheritance
Ability to create new data types from existing data types
Polymorphism
Ability to use same expression to denote different operations
Chapter 2: Methods & Exception Handling
- Overloading Methods
Create another method with same name but different parameter list
- Try-Catch Block
Try block
Monitor errors
Catch block
When exception occurs, exception is thrown out of try block &
caught by catch statement
- Finally Clause
Used when there are statements that always to be executed, whether
try block exits normally or not
Chapter 3: Arrays
- Definition of Array
A data structure that represents a collection of same types of data
- Heap
Stores array in an area of memory
Used for dynamic memory allocation
Chapter 4: Objects & Classes
- Procedural Paradigm VS Object-oriented Paradigm
Procedural Object-oriented
Data & methods are loosely Couples data & methods together
coupled into objects
Software design focuses on Software design focuses on
designing methods objects & operations on objects
As data & operations on the data Places data & operations
are separate, this methodology pertaining to the data within a
requires sending data to methods single entity called an object
- Definition of Objects & Classes
Objects
An object has both a state & a behaviour
Classes
Define objects of same type
A class uses
♪ Variables to define data fields
♪ Method to define behaviours
Class Circle
Object -radius: double
+Circle (radius: double)
Method +getRadius (radius: double): void
+getRadius(): double
- Constructors
A method invoked to construct objects
No-arg constructor
A constructor with no parameters
Must have same name as the class itself
- Instance Variables & Methods
Instance variables
Belong to specific instance
♪ Not share among instances
Used by Objects to store their states
Variables which are defined without Static keyword & are
Outside any method declaration
Instance methods
Invoked by an instance of the class
♪ Not share among instances
Belong to Object of class not to the class
Every individual Object created from class has its own copy of
the instance method(s) of class
Class with
method
Another class
Create instance of class - Can be same package /
to be called different package
Calling instance method
from another class
- Static Variables, Constants & Methods
Static variables
Shared by all instances of the class
Accessed using the object name or class name
All objects will be affected if one of them changes the static
variable
Static methods
Not tied to a specific object
Class with
method
Another class
- Can be same package /
different package
Static constants
Final variables shared by all the instances of the class
- Instance Method VS Static Method
Can Instance Instance Static Static
Access Directly Method Variable Method Variable
Instance Method / / / /
Static Method Must use Must use / /
reference to reference to
object object
- Visibility Modifiers
Restricting access to a class’ members
Helps prevent misuse of an object
4 Visibility Modifiers
(Accessed From) Same Same Subclass in Different
Modifier Class Package different Package Package
Public / / / / Unrestricted Access
Protected / / / X
Default / / X X Restricted Access
within a Package
Private / X X X Restricted Access
within a Class
- Private Data Fields
Aim
To protect data fields from direct modification
♪ Restricted access by an object through a direct reference
outside the class
To make the class easy to maintain
Retrieve & modify data field
By providing accessor & mutator methods
- Accessor & Mutator Methods
Accessor Methods
To read private properties
Get Methods
Syntax:
♪ public returnType getPropertyName()
Ex. public double getPrice() {
return price;
}
Mutator Methods
To modify private properties
Set Methods
Syntax:
♪ public void setPropertyName (dataType propertyValue)
Ex. public void setPrice (double price) {
this.price = price;
}
- Passing Objects to Methods
Passed by Value
Value of the variable in calling method is passed to the method’s
parameter
2 Primitive Types
Different primitive types cause different effect of passed by value
Primitive-typed Argument
♪ Changes to the parameter
X affect variable in calling method
Referent-typed Argument
♪ Value of variable is reference to the object
♪ Changes to the parameter
Will affect variable in calling method
- Aggregation
An object can contain another object
Aggregation models has-a relationship
While superset class destroyed, all its subset class still exist
- Composition
An object is exclusively owned by an aggregating object
When the superclass is destroyed, all its subset class is destroyed
Ex. A name belongs to only 1 student
- 3 Foundational Principles of OO
Encapsulation
Combine data & operations into a single unit
A class should use private modifier to hide its data from direct
access by clients
♪ Can use get & set methods to access to private data
Advantages:
♪ Links data with code that manipulates it
♪ Provide means by which access to members can be controlled
Keeps both data & code safe from external interference &
misuse
Inheritance (Chapter 6)
Used to model is-a relationship
Create new data based on existing data types
A subclass
♪ An extension of its superclass
♪ Inherits all the data members & methods of its superclass
Advantages:
♪ Allows to reuse the code
♪ Allows to build a hierarchy among classes
Polymorphism (Chapter 6)
Use same expression to denote different operations
Allows methods of same name behave differently within a class
family
♪ Allows 1 method name to be used throughout the class family
Advantages
♪ Easily extensible & maintainable
Chapter 6: Inheritance & Polymorphism
- Method Overriding
When a method in a subclass has the same parameter & name as a
method in its superclass
Method in subclass will override method in superclass
- Overriding VS Overloading
Overriding
Occurs when parameter & name of the 2 methods are identical
Overloading
Occurs when 2 methods have same name but different parameter
Chapter 7: Abstract Classes & Interfaces
- Abstract class
Can’t be instantiated
Used as a base class for defining new subclasses
- Abstract method
Can’t be contained in a non-abstract class
Can invoke abstract methods on parameters of the superclass
- Interface
Data fields must be public final static
Data must be constants
Methods must be public abstract
- Interfaces VS Abstract Classes
Variables Methods Constructors
Abstract Class No restrictions No restrictions Invoked by subclasses
Interface All variable All methods No constructor
must be public must be public
static final abstract instance
(i.e. Constants) methods
Chapter 8: OO Design & Patterns
- Aggregation
An object can contain another object
Aggregation models has-a relationship
While superset class destroyed, all its subset class still exist
- Composition
An object is exclusively owned by an aggregating object
When the superclass is destroyed, all its subset class is destroyed
Ex. A name belongs to only 1 student
- Dependency
Describes a relationship between 2 classes where one (called client)
uses the other (called supplier)
Ex. Calendar class uses Data
x Calendar Date
(Client) (Supplier)
- Dependency VS Association
Similarity:
Both describe 1 class as depending on another
Association Dependency
Relationship Stronger Weaker
Description The state of object The client object &
changes when its the supplier object are
associated object loosely coupled
changed
Java Implementation- * Data fields * Methods
- * Methods
- Class Design Guidelines
Cohesion
A class should describe a single entity
All the class operations should logically fit together to support a
coherent purpose
Consistency
Always follow standard Java programming style & naming
conventions
Always provide a public no-arg constructor
Encapsulation
A class should use private modifier to hide its data from direct
access by clients
♪ Can use get & set methods to access to private data
Clarity
Implement cohesion, consistency & encapsulation to help achieve
clarity
Classes are designed for easy to reuse
Completeness
Classes are usually designed for use by many different customers
♪ To make a class useful in a wide range of applications
Instance VS Static
Instance variables
♪ Belong to specific instance
Not share among instances
Static variables
♪ Shared by all instances of the class
♪ Accessed using the object name or class name
♪ All objects will be affected if one of them changes the static
variable
Inheritance VS Aggregation
Inheritance
♪ Used to model is-a relationship
♪ Create new data based on existing data types
♪ A subclass
An extension of its superclass
Inherits all the data members & methods of its superclass
Aggregation
♪ An object can contain another object
♪ Aggregation models has-a relationship
♪ While superset class destroyed, all its subset class still exist
Interface VS Abstract Classes
Variables Methods Constructors
Abstract Class No restrictions No restrictions Invoked by subclasses
Interface All variable All methods No constructor
must be public must be public
static final abstract instance
(i.e. Constants) methods
- Packages
Used to organize classes
Advantages:
To locate classes
To avoid naming conflicts
To distribute software conveniently
To protect classes