Encapsulation Inheritance Polymorphism
Encapsulation Inheritance Polymorphism
to OO
- Structured Design
Divide a problem into smaller subproblems
Aka. Top-down design / Stepwise design / Modular programming
- 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
Class Circle
- 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
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
- 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
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
- 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
- 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
- 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
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
- Packages
Used to organize classes
Advantages:
To locate classes
To avoid naming conflicts
To distribute software conveniently
To protect classes