Slides 15.12.2023
Slides 15.12.2023
Pramit Mazumdar
12/15/2023 1
Challenges in Software Engineering
Why is it important to study object orientation?
12/15/2023 2
Challenges in Software Engineering
Complex Software:
Analysis
Design
Implementation
Maintenance
12/15/2023 3
Challenges in Software Engineering
However, mere use of a OOPL does not guarantee
the above
12/15/2023 4
Challenges in Software Engineering
Most of the problems start at the specification
level
12/15/2023 5
Challenges in Software Engineering
Similarly there are software which are generic in
nature
12/15/2023 6
Challenges in Software Engineering
Typically a software engineering is responsible for
engineering a software that can be used and liked
by the end users
12/15/2023 7
Challenges in Software Engineering
According to Standish Group’s Annual CHAOS report, 66%
of technology projects (based on the analysis of 50,000
projects globally) end in partial or total failure*
12/15/2023 * https://www.atlascode.com/blog/why-software-projects-fail/ 8
Why Software Fails !!
COMPLEXITY …. complexity in various domain
12/15/2023 9
Why Software Fails !!
Changes from People
Usually the process of constructing something can be automated
Developer changes
One may not follow the approach followed by an ex-developer of the same
project
Etc.
12/15/2023 10
Software Engineering
Is Software engineering
A manufacturing process, or
12/15/2023 11
Target
Software Development -> Software Construction
In future can we
Construct a software
12/15/2023 12
Target
Current SOA says that to achieve the above we
need
Structured analysis
Modelling
Design
Implementation
12/15/2023 13
Why this course
Object Oriented Analysis and Design
12/15/2023 14
Types of Software
Personal or limited use Software
Industrial-strength Software
12/15/2023 15
Types of Software
Industrial-strength Software
Long life-span
12/15/2023 16
Software Complexities
The complexity of the problem domain
12/15/2023 17
Problem domain
Domains are difficult to understand
Usability
Performance
Cost
Survivability
Reliability
Users do not understand the non-functional requirements
Implicit in nature !!
12/15/2023 18
Problem domain
Communication gap between Users and
Developers
12/15/2023 19
Managing the Development Process
Illusion of Simplicity
12/15/2023 20
Flexibility possible through a Software
Components required to build a Software is not
readily available
12/15/2023 21
Behavior of Discrete system
Discrete system changes its state abruptly at
discrete points, consists of multiple states
12/15/2023 22
Behavior of Discrete system
12/15/2023 23
Behavior of Discrete system
12/15/2023 24
Complex systems are Hierarchic
PC
Registers
Gates
12/15/2023 25
Complex systems are Hierarchic
Each level of hierarchy represents a Layer of
abstraction
12/15/2023 27
Complex systems are Hierarchic
All systems are composed of interrelated sub-
systems
12/15/2023 28
Five attributes of a Complex system
Hierarchic structure
Relative primitives
Primitives – section of code which can be used to develop
more sophisticated modules
12/15/2023 29
Relative properties
Primitives are chosen by the developer
It is a Subjective choice
12/15/2023 30
Separation of Concerns
Hierarchic systems are
Decomposable = consists of independent elements, so can be
decomposed
12/15/2023 31
Common Patterns
Processor is found in computers, cars, mobiles,
etc.
12/15/2023 32
Stable Intermediate Forms
It is extremely difficult to design a complex system
correctly in one go
12/15/2023 33
Types of Complexities
Organized (Canonical Form)
Decomposition hierarchy
Abstraction hierarchy
Class and object structure
Canonical form
Disorganized
12/15/2023 34
Organized Complexity
We are able to find a structure
12/15/2023 35
Limitation of Human Capacity for Dealing Complexity
12/15/2023 36
Types of Hierarchy
We know that complex systems are hierarchical in
nature
Abstraction = is-a
12/15/2023 37
Decomposition Hierarchy
PC
Registers
Gates
12/15/2023 38
Decomposition Hierarchy
PC can be broken down to components such as
CPU, HDD, Mouse, Keyboard, etc.
12/15/2023 39
Abstraction Hierarchy
CPU
Celeron …. ….
12/15/2023 40
Abstraction Hierarchy
Specialization of CPU is not mentioned in previous
example
CPU can be
Pentium
Pentium + MMX
Celeron
12/15/2023 41
Abstraction Hierarchy
Pentium is-a CPU
12/15/2023 43
Decomposition Hierarchy
PC
Registers
Gates
12/15/2023 44
Abstraction Hierarchy
CPU
Celeron …. ….
12/15/2023 45
Abstraction Hierarchy
Specialization of CPU is not mentioned in previous
example
CPU can be
Pentium
Pentium + MMX
Celeron
12/15/2023 46
Abstraction Hierarchy
Pentium is-a CPU
12/15/2023 47
Class and Object Structures
12/15/2023 48
Class – Abstraction – IS-A relationship
Class structure shows IS-A relationship
Example:
C1 = CPU
C2 = Pentium
C3 = Mac
C7 = Celeron
Celeron Pentium
12/15/2023 49
Class – Abstraction – IS-A relationship
Celeron IS-A Pentium
12/15/2023 50
Decomposition Hierarchy
PC
Registers
Gates
12/15/2023 51
Decomposition Hierarchy
PC can be broken down to components such as
CPU, HDD, Mouse, Keyboard, etc.
12/15/2023 52
Object – Decomposition – HAS-A relationship
12/15/2023 53
Object – Decomposition – HAS-A relationship
12/15/2023 54
Canonical Form of a Complex System
12/15/2023 55
Canonical Form of a Complex System
12/15/2023 56
The Role of Decomposition
Algorithmic approach
Divide-and-conquer
12/15/2023 57
Algorithmic Decomposition
12/15/2023 58
Object-oriented Decomposition
12/15/2023 59
Object-oriented Decomposition
12/15/2023 60
Object-oriented Decomposition
Master File can get formatted update from the File Updates
abstraction
12/15/2023 61
Object-oriented Decomposition
Master File can get formatted update from the File Updates
object
12/15/2023 62
Object-oriented Decomposition
12/15/2023 63
Object-oriented Decomposition
Objects
Objects
Objects
12/15/2023 64
Objects provides services
f21 f22
O1 O2
f11
f23
f12
f31 f32
O3
f33
f34
12/15/2023 65
Objects communicate among themselves
f21 f22
O1 O2
f11 <O2:f23> f23
f12
f32
O3
f31
f33
Service should
f34 preserve concurrency
12/15/2023 66
Object-Oriented Decomposition
Therefore;
Objects can perform multiple tasks or services or operations
12/15/2023 67
Evolution of Design Methods
Data-driven design
Object-oriented design
12/15/2023 68
Top-down structured design
12/15/2023 69
Data-driven design
12/15/2023 70
Object-oriented design
12/15/2023 71
Algorithmic vs Object oriented Decomposition
Algorithmic Object-oriented
Highlights the ordering of events Emphasizes on Agents (clients /
servers)
Typically develops Larger Systems Yields smaller systems by reusing
methodologies
Lower re-usability characteristics Large re-usability characteristics
Less resilient and more risky to build More resilient to change and reduces
complex systems risk of building complex systems
Unable to reduce complexity By following the approach of
“separation of concerns” is reduces
the complexity of a system
significantly
Low concurrency High concurrency and Distributed in
nature.
12/15/2023 72
Abstraction
12/15/2023 73
Abstraction
12/15/2023 74
Recap: Object and Class structure
These three are the main tool to use when we try to design a
complex system
12/15/2023 75
What we mean by “Design”?
12/15/2023 76
Evolution of Programming Languages
First-generation (1954-1958)
One global data and subprograms
Error in one part effects rest of the system
Second-generation (1959-1961)
One global data and subprograms. Subprograms have individual modules
Fails in programming-in-large scenario and design
Third-generation (1962-1970)
Structured, Modular, Abstraction
Modules were separately compiled and hence no track of errors
Fourth-generation (1970-1980)
Large data handling with Database management
12/15/2023 77
Evolution of Programming Languages
Object-orientation (1980-1990)
Object-based models
Exception handling
Task-specific
12/15/2023 78
Foundations of Object Models
12/15/2023 79
Unified Modelling Language (UML)
12/15/2023 80
UML History
Unified Modeling Language (UML)
Unified Modeling Language
92
What is a Use Case?
93
Benefits of Use Cases
94
UML Use Case Diagrams
95
UML first pass: Use case diagrams
Classifier
Use Case
Actor.
System boundary
Actor
ReadTime
SetTime
WatchUser WatchRepairPerson
ChangeBattery
98
System (cont.)
99
Actor
Example:
Policeman – Enters data
100
Actor (cont.)
101
Actor Icons
External system
Physical environment
Passenger
An actor has a unique name and an
optional description
Examples:
Passenger: A person in the train
GPS satellite: Provides the system with
GPS coordinates
Use Case
Actor.
System boundary
Actor
ReadTime
SetTime
WatchUser WatchRepairPerson
ChangeBattery
12/15/2023 112
Object Oriented Analysis (OOA)
12/15/2023 113
Object Oriented Analysis (OOA)
Identifying Objects
Identifying Structure
Identifying Attributes
Identifying Associations
Defining services
12/15/2023 114
Object Oriented Design (OOD)
12/15/2023 115
Object Oriented Design (OOD)
12/15/2023 116
Object Oriented Design (OOD)
Classification of operations
Who is going to use?
What are the restrictions?
Algorithm design
At what point during the design phase we should think about the algorithm
which would be running in the system?
12/15/2023 117
Object Oriented Programming (OOP)
OOD gives us the opportunity to put the concepts learnt from OOA
into the system
OOD deals with the system architecture and ways in which the
observations from OOA can be induced into the system
OOD also deals with handling the limitations of the system while
inducing the observations from OOA.
12/15/2023 118
Object Oriented Programming (OOP)
Now using the inputs from OOA and OOD subsequently we need to
build the real-life system
12/15/2023 119
Object Oriented Programming (OOP)
On the other hand, Python is extremely slow in processing
It depends upon
What we need to do
12/15/2023 120
Object Oriented Programming (OOP)
On the other hand, Python is extremely slow in processing
It depends upon
What we need to do
What are the constraints in the system we are developing
What are the features of the chosen OOP language
12/15/2023 121
Object Oriented Programming (OOP)
Re-usability
Reusing some facilities rather than building them again and
again
Data redundancy
12/15/2023 122
Object Oriented Programming (OOP)
Code maintenance
Security
12/15/2023 123
Object Oriented Programming (OOP)
Design benefits
Better productivity
12/15/2023 124
Class - Object
Abstraction hierarchy gives Class structure
How many classes?
How many objects?
Object of which classes?
Why we create object of those classes?
12/15/2023 125
Object Oriented Programming (OOP)
Three important parts of OOP
Object
Class [Type]
Inheritance
Data
12/15/2023 126
Class
Objects have an associated Type which is known as the
Class
12/15/2023 127
Class – Objects – Methods
Object oriented programs deal with Objects
12/15/2023 128
Class – Objects – Methods
Here we write codes in separate blocks
12/15/2023 129
Class – Objects – Methods
Method -1 Method -1 Method -1
Method - 2 Method – 2
Method - 3
Class 1 Class 2
Class 3
12/15/2023 130
Class
Class is a structure that defines the data and
methods to work on
12/15/2023 131
Methods
Class consists of collection of methods
12/15/2023 132
Objects
Objects are instances of a class or an executable
copy of a class
12/15/2023 133
First program
class test
{
public static void main (String args[ ])
{
System.out.println (“Hello World!!”);
}
}
class test
Declares a class named as test
Contains a single method “main( )”
“class” is a keyword
“test” is the identifier which provides name to the new class.
12/15/2023 134
First program
public static void main(String args[ ])
public
It is an Access specifier
It is used to declare the method main( ) as unprotected
Makes it accessible to all other classes
static
Every method is accessed using the object of a particular class
Static method can be accessed directly without using objects
main( ) should be declared static as it is the first method from which
execution starts
Object creation for the main( ) method container class is not possible
prior to execution
void
main( ) do not return any value
String args[ ]
Handling command line arguments.
12/15/2023 135
First program
class sample
{
void display( )
{
System.out.println(“Pramit”);
}
}
class test
{
public static void main(String args[])
{
sample SS = new sample( );
SS.display( );
}
}
12/15/2023 136
Object creation
sample SS;
SS = new sample( );
sample SS
Reference created
Storage space taken
Storage space not yet instantiated
SS = new sample( )
Storage space instantiated
Sample class object created.
12/15/2023 137
Inheritance
Types [classes] may inherit attributes from supertypes
[superclasses]
12/15/2023 138
How are OOA, OOD, and OOP related?
OOA is concerned with developing an object model that capture the
requirements
OOD is concerned with translating OOA model into a specific model that
can be implemented by a software
OOP is concerned with realizing the OOD model using OOP language such
as Java, C++, etc.
12/15/2023 139
Important Topics
Understand Abstraction
Understand Encapsulation
12/15/2023 140
Elements of Object Models
Abstraction
Encapsulation
Modularity
Hierarchy
12/15/2023 141
Elements of Object Models
Typing
Concurrency
Persistence
12/15/2023 142
Abstraction
Example:
12/15/2023 143
Abstraction
12/15/2023 144
Abstraction
12/15/2023 145
Abstraction
12/15/2023 146
Abstraction
Example
Class-1 is having 10 properties
Object-1 is created for Class-1
Object-1 would have all 10 properties
Now You want to allow User-1 to access the system using
Object-1
Also you want User-1 not to access property 5-10 which are
there in Class-1
You create Class-2 and pass property 5-10 of Class-1 to
Class-2
Thus make Class-2 as generalized class for Class-1
(Inheritance)
Thus User-1 using Object-1 of Class-1 gets to use all
properties without visualizing the working of properties 5-10
which are not written in Class-1.
12/15/2023 147
Abstraction
Hierarchy of models
A model in a layer is abstraction of the lower layer of
models
Higher layer models implement the model in the layer.
12/15/2023 148
Abstraction Hierarchy: Living Organisms
12/15/2023 149
Abstraction Hierarchy: LMS
12/15/2023 150
Abstraction
Executive, Lead, and Manager are a type of Employee
12/15/2023 151
Abstraction
All lower level abstraction in the hierarchical structure are
known as the Specialization
12/15/2023 152
Abstraction
Example:
Generalized “Employee” have 4 responsibilities which are
common for Executive, Lead, and Manager
12/15/2023 153
Abstraction Hierarchy: LMS
12/15/2023 154
Abstraction
More commonalities can be observed
Commonalities ??
Approve Approve
Executive Regret Lead Regret Manager
Revoke Revoke
12/15/2023 155
Abstraction
LMS
Lead Manager
12/15/2023 156
Multi Level Hierarchy
12/15/2023 157
Elements of Object Models
Four major elements of the Object Model
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency
Persistence
12/15/2023 158
Encapsulation
Primary task is to encapsulate
12/15/2023 159
Encapsulation: Example
12/15/2023 160
Encapsulation: Example
12/15/2023 161
Encapsulation: Example
Structure is encapsulated in the implementation
Store for the elements
static or dynamic array
Single linked list
Deque
Marker for the top element
Array index
List pointer
Encapsulated in Deque
Hidden from the external world
12/15/2023 162
Encapsulation
For an Abstraction, the Encapsulation concept separates
12/15/2023 163
Encapsulation
Remember that Encapsulation or hiding of the complex
concepts are important
12/15/2023 164
Encapsulation
So the concept is similar to Client-Server model where
Interface is what the Client gets to see and Server is the
implementation details which remains encapsulated
12/15/2023 166
Encapsulation: Example
12/15/2023 167
Abstraction Hierarchy: LMS
12/15/2023 168
Abstraction vs Encapsulation
Abstraction Encapsulation
Solves the problem in the design level Solves the problem in the
implementation level
Hide the unwanted data and expose Hide the code and data into a single
useful data unit to protect the data from outside
world
Allows us to focus on what the object Hide the internal details or mechanics
does instead of how it does it of how an object does something
Provides outer layout, in terms of Provides inner layout used in terms of
design outer
Example: Look of a Mobile phone has Example: Implementation detail of a
a display screen and keypad buttons mobile phone deals with how keypad
to dial a number. button and display screen connect with
each other.
12/15/2023 169
Elements of Object Models
Four major elements of the Object Model
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency
Persistence
12/15/2023 170
Modularity
Modularity depicts how the code and data are organized
Module characteristics
12/15/2023 171
Modularity: Example
12/15/2023 172
Modularity: Example
12/15/2023 173
Modularity: Example
12/15/2023 174
Modularity: Example
12/15/2023 175
Modularity: Example
12/15/2023 176
Modularity: Example
12/15/2023 177
Modularity: Example
12/15/2023 178
Modularity: Example
Modularity ensures
12/15/2023 179
Modularity
It is always easier to look into the system when modularized
12/15/2023 180
Modularity: Objectives
Modular Decomposition
We only put together a set of code into one module which have
some sort of well-defined functionality
Modular Composability
Modularization should be done in such a way that we can
reconstruct it back
12/15/2023 181
Modularity: Objectives
Modular Understandability
12/15/2023 182
Modularity: Objectives
Modular Continuity
12/15/2023 183
Modularity: Objectives
Modular Protection
12/15/2023 184
Modularity: Challenges
Decomposition into smaller module may be difficult
12/15/2023 185
Modularity: Example
Consider a Distributed System
Runs on a distributed set of processors
Uses a message passing mechanism to coordinate the activities of
different programs
Has several hundreds of event & few thousand kinds of messages
12/15/2023 186
Intelligent Modularization
Group logically related abstraction
Ease of change
Possibility to change the implementation strategy of an individual module
12/15/2023 187
Elements of Object Models
Four major elements of the Object Model
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency
Persistence
12/15/2023 188
Hierarchy
Hierarchy is a ranking or ordering of abstractions
12/15/2023 189
Hierarchy
Two most important hierarchies in a complex system are
Class structure or is-a hierarchy (Abstraction or IS-A)
12/15/2023 190
Hierarchy
Hierarchy provides an opaque barrier to hide the methods
and state
12/15/2023 191
Single Inheritance
A single superclass is inherited by a single subclass
12/15/2023 192
Multi-level Inheritance
12/15/2023 193
Multiple Inheritance
For certain abstractions, it is important to provide
inheritance from multiple superclasses
studies teaches
Student Teacher
studies +
teaches
Teaching Assistant
12/15/2023 194
Multiple Inheritance: Challenges
A very critical and important concept related to Hierarchy
12/15/2023 195
Hierarchical Inheritance
More than one sub classes are created from the same super
class
12/15/2023 196
Hybrid Inheritance
It may be a combination of
12/15/2023 197
Single Inheritance - Example
class one public class Single_Inheritance
{ {
int x; public static void main(String[] args)
public void print_1( ) {
{ two g = new two( );
x = 10; g.print_1( );
System.out.println("CSE"); g.print_2( );
}
System.out.print(g.x);
}
}
class two extends one
{ }
public void print_2( )
{ OUTPUT:
System.out.println("IIIT Vadodara"); CSE
} IIIT Vadodara
} 10
12/15/2023 198
Single Inheritance – Class Diagram
One
+ x: int
+ print_1( )
Single_Inheritance
+ main( ): void
Two
+ print_2( )
12/15/2023 199
Multilevel Inheritance
A child class derives member variables
and methods from another derived
class.
A: Parent class
B: Child class
C: Child class
12/15/2023 200
Multilevel Inheritance - Example
class X class Z extends Y {
{ int k;
int i; Z( ) {
X( ) k=9; j++;
{ }
i=5; }
} public class Multilevel_Inheritance{
} public static void main(String args[]) {
class Y extends X X a = new X();
{ Y b = new Y();
int j; Z c = new Z();
Y( ) System.out.println(a.i);
{ System.out.println(b.i + b.j);
j=7; i++; System.out.println(c.i + c.k);
} System.out.println(c.i + c.j + c.k);
} } }
12/15/2023 201
Multilevel Inheritance – Class Diagram
X
+ i: int
+ X( )
Multilevel_Inheritance
Y
<<create>>
+ j: int
+ Y( ) + main( ): void
Z
+ k: int
+ Z( )
12/15/2023 202
Hierarchical Inheritance
One base class is derived by many child
classes.
A: Parent class
12/15/2023 203
Hierarchical Inheritance - Example
class Shape class rectangle extends Shape {
{ int k;
int i; rectangle( )
Shape( ) {
{ k=7;
i=5; }
} }
} class Hierarchical_Inheritance {
public static void main(String args[]) {
class square extends Shape Shape a = new Shape();
{ square b = new square();
int j; rectangle c = new rectangle();
square( ) System.out.println(a.i);
{ System.out.println(b.i * b.j);
j=6; System.out.println(c.i + c.k);
} } } }
12/15/2023 204
Hierarchical Inheritance – Class Diagram
Shape
+ i: int
+ Shape( )
square rectangle
+ j: int + k: int
+ square( ) + rectangle( )
Hierarchical_Inheritance
+ main( ): void
12/15/2023 205
Constructors
class Shape class rectangle extends Shape {
{ int k;
int i; rectangle( )
Shape( ) {
{ k=7;
i=5; }
} }
} class Hierarchical_Inheritance {
public static void main(String args[]) {
class square extends Shape Shape a = new Shape();
{ square b = new square();
int j; rectangle c = new rectangle();
square( ) System.out.println(a.i);
{ System.out.println(b.i * b.j);
j=6; System.out.println(c.i + c.k);
} } } }
12/15/2023 206
Constructors
It is a method which gets initialized at the time of object
creation
...
}
Tracing a constructor call
What happens when the following call is made?
p1 x y
This declares local variables with the same name as the fields,
rather than storing values into the existing variables. The existing
attributes remain 0.
...
}
Constructor example
public class PointMain3 {
public static void main(String[] args) {
// create two Point objects
Point p1 = new Point(5, 2);
Point p2 = new Point(4, 3);
} }
Multiple constructors
A class can have multiple constructors.
Each one must accept a unique set of parameters.
public Point() {
x = 0;
y = 0;
}
public Point(int initialX, int initialY) {
x = initialX;
y = initialY;
}
p2.translate(2, 4);
System.out.println("p2: (" + p2.x + ", " + p2.y + ")");
}
}
Types of Constructors
Parameterized Constructor
Default constructors
Copy constructors
Constructor overloading
Copy Constructor
A copy constructor is a method/constructor that
initialize an object using another object within the
same class
when you have a complex object with many attributes it is much simpler
to use the copy constructor
if you add an attribute to your class, you just change the copy
constructor to take this new attribute into account instead of changing
every occurrence of the other constructor.
Shallow copy
Deep copy
Copy Constructor
Shallow copy
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency
Persistence
12/15/2023 222
Minor Elements
These are not essential elements
12/15/2023 223
Typing
This is NOT the Word/Code typing speed !!
It is important because
Usually in programming we have variables
Variable have a definite type or datatype
We know what type of data we need to store and accordingly define the types
However, for an object the scenario is different
It is not going to have a single value
It is going to represent a class
Inherent properties of the class which the object refers to needs to be understood
Inherent properties of the class which is being referred by an object is the Type of
the Object.
12/15/2023 224
Typing
Type derives from the theory of abstract data types (ADT)
ADT consists of
Domain
Range of values that can be stored
Operations
What operations can be performed with that value
Axioms
How the operations which can be performed, interplay among themselves
12/15/2023 225
Typing
Built-in (Primitive) Types
Int
Double
char
bool
12/15/2023 226
Type of Programming Languages
A programming language may be
12/15/2023 227
Type of Programming Languages
Dynamically typed – dynamic binding or late binding
12/15/2023 228
Type of Programming Languages
Strongly Typed
Weakly Typed
Untyped
12/15/2023 229
Polymorphism
Polymorphism exists when dynamic typing and inheritance
interact
dynamic typing:
X = 2;
X = 2.5;
12/15/2023 230
Signatures
Signatures
In any programming language, a signature is what
distinguishes one function or method from another
233
Polymorphism
This feature is known as Polymorphism
234
Java Program Execution Steps
235
Polymorphism
Compile time polymorphism (Static/Early binding)
236
Overloading
class Test {
public static void main(String args[]) {
void myPrint(5);
void myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
237
Why overload a method?
So you can use the same names for methods that do essentially the
same thing
Example: println(int), println(double), println(boolean),
println(String), etc.
238
DRY (Don’t Repeat Yourself)
When you overload a method with another, very similar
method, only one of them should do most of the work:
void dict_Example( ) {
System.out.println("first = " + first + ", last = " + last);
for (int i = first; i <= last; i++) {
System.out.print(dictionary[i] + " ");
}
System.out.println();
}
void dict_Example(String s) {
System.out.println("At checkpoint " + s + ":");
dict_Example();
}
239
Legal assignments
class Test {
public static void main(String args[]) {
double d;
int i;
d = 5; // legal
i = 3.5; // illegal
i = (int) 3.5; // legal
}
}
Narrowing is legal
Widening is illegal (unless you cast)
240
Legal method calls
class Test {
public static void main(String args[]) {
myPrint(5);
}
static void myPrint(double d) {
System.out.println(“Output: ” + d);
}
}
Output 5.0
241
Illegal method calls
class Test {
public static void main(String args[]) {
myPrint(5.2);
}
static void myPrint(int i) {
System.out.println(i);
}
}
242
Method Overloading
It is a technique in which a single class contains
Multiple methods
Of same name
But different parameter list
For distinguishing among themselves
Object of the class is used to call each method.
class test{
void display() { }
void display(int x) { }
void display(int x, int y) { }
}
Constructor Overloading
It is a technique in which a single class contains
Multiple constructors
Of same name
But different parameter list
For distinguishing among themselves
Object of the class is not used to call the constructors.
class test{
test( ) { }
test(int x) { }
test(int x, int y) { }
}
Java uses the most specific method
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(double d) {
System.out.println("double: " + d);
}
static void myPrint(int i) {
System.out.println("int: " + i);
}
}
int: 5
double: 5.0
245
Operator Overloading
Methods and Operators can be overloaded;
Method overloading (includes constructors)
Operator overloading
Examples: t1 / t2 or t1 == t2
12/15/2023 247
Operator Overloading - Example
12/15/2023 248
Operator Overloading
Almost all operators can be overloaded
sizeof
member selector - .
ternary operator - ?:
12/15/2023 249
Operator Overloading - Syntax
12/15/2023 250
Operator Overloading - Restrictions
Precedence and Associativity of an operator
cannot be changed.
12/15/2023 251
/ Operator Overloading - Example
class test void test::operator/(test t2)
{ {
int n; cout << n / t2.n;
public: }
test( )
{ int main( ) OUTPUT:
cin >> n; {
} test t1, t2;
10 <= t1.n
t1 / t2;
void operator/(test); 5 <= t2.n
return 0;
}; } 2
12/15/2023 252
<< Operator Overloading - Example
class test {
int n;
public:
test( ) {
cin>>n;
}
void operator/(test);
friend ostream& operator<< ( ostream&, test& );
};
int main( )
{
test t1,t2;
t1 / t2;
cout << t1;
return 0;
}
12/15/2023 254
== Operator Overloading - Example
class test {
int n;
public:
test( ) {
cin>>n;
}
void operator/(test);
friend bool operator== ( test, test );
};
int main( )
{
test t1,t2;
t1/t2;
cout << endl << ( t1 == t2 );
return 0;
}
12/15/2023 256
Multilevel Inheritance
A child class derives member variables
and methods from another derived
class.
A: Parent class
B: Child class
C: Child class
12/15/2023 257
Method Overriding
If subclass (child class) has the same method as
declared in the parent class, it is known as method
overriding
Advantage:
Provide specific implementation of a method that is already provided by
its super class
Rules:
Same name as method in parent class
Same parameter as in the parent class
Must be in a IS-A relationship (inheritance)
12/15/2023 258
Method Overriding - Example
class Bank{ class AXIS extends Bank{
int getRateOfInterest( ) int getRateOfInterest( )
{ return 0; } { return 9; }
} }
public class Method_Overriding{
class SBI extends Bank{ psvm(S a[]){
int getRateOfInterest( )
{ return 8; } SBI s=new SBI( );
} ICICI i=new ICICI( );
AXIS a=new AXIS( );
class ICICI extends Bank{ S.O.P(“SBI”+ s.getRateOfInterest( ));
int getRateOfInterest( ) S.O.P(“ICICI”+i.getRateOfInterest());
{ return 7; } S.O.P(“AXIS”+a.getRateOfInterest());
}
}
}
12/15/2023 259
Method Overloading vs Overriding
Method Overloading Method Overriding
12/15/2023 260
Access Specifiers
Types of Access Specifiers:
Public
Default (Friendly)
Protected
Private
12/15/2023 261
Access Specifiers or Visibility Modes
Provides a restriction over the scope of a
variable/entity of a program
Class
Sub-class
Package
12/15/2023 262
Public Access Specifier
Public attributes/methods are accessible from
anywhere in the program
12/15/2023 263
Protected Access Specifier
WHO CAN ACCESS
1. All members within the same class
2. All members within sub-classes
3. Sub-classes present in the same package
4. Classes of same package which are not related
5. Sub-classes present in other packages.
12/15/2023 264
Default (Friendly) Access Specifier
WHO CAN ACCESS
1. All members within the same class
2. All members within sub-classes
3. Sub-classes present in the same package
4. Classes of same package which are not related.
12/15/2023 265
Private Protected Access Specifier
WHO CAN ACCESS
1. All members within the same class
2. All members within sub-classes
3. Sub-classes present in the same package
5. Sub-classes present in other packages
12/15/2023 266
Private Access Specifier
WHO CAN ACCESS
1. All members within the same class
12/15/2023 267
Hierarchical Inheritance – Class Diagram
Shape
- i: int
+ Shape( )
square rectangle
- j: int - k: int
+ square( ) + rectangle( )
Hierarchical_Inheritance
+ main( ): void
12/15/2023 268
Multiple Inheritance
A child class derives member variables
and functions from multiple parent
classes.
C: Child class
12/15/2023 269
Multiple Inheritance – Example C++
class One { void show( ) {
protected: cout << "Vadodara" << endl;
int x; }
public: };
One( ) { x = 5; } class Three : public One, public Two
void disp( ) {
{ public:
cout << “IIIT" << endl; void printValues( ) {
} cout << x + y << endl;
}; }
class Two { };
protected: int main( ) {
int y; Three t1;
public: t1.printValues( );
Two( ) { y = 50; } t1.disp( ); t1.show( );
}
12/15/2023 270
Hybrid Inheritance
Combination of two or more types of
inheritances.
A: Parent class
D: Child class
12/15/2023 271
Hybrid Inheritance – Problem
class A
public: int x
class D : public B, C
AMBIGUOUS int x
12/15/2023 272
Hybrid Inheritance – Solution in C++
Two ways to counter this problem in C++:
12/15/2023 273
Hybrid Inheritance – Solution-1
class A
public: int x
class D : public B, C
B :: int x
12/15/2023 274
Virtual Base Class
class A
public: int x
class D : public B, C
int x
12/15/2023 275
Constructor calling - Inheritance
Base class constructor is called by default
12/15/2023 276
Constructor calling - Inheritance
class First public class
{ ConstructorCalling_Inheritance
First( ) {
{ public static void main(String args[])
System.out.println("IIIT"); {
} Second S = new Second( );
} }
class Second extends First }
{
Second( ) OUTPUT:
{
System.out.println("Vadodara"); IIIT
} Vadodara
}
12/15/2023 277
Program to Class diagram
public class Point {
public class MultipleConstructor
int x; int y; {
public Point( ) public static void main(String[]
{ args)
x = 0; {
y = 0; Point p1 = new Point(5, 2);
} Point p2 = new Point(4, 3);
public Point(int initialX, int initialY) Point p3 = new Point();
{
x = initialX; p2.translate(2, 4);
y = initialY;
} }
}
public void translate(int dx, int dy)
{
x = x + dx;
y = y + dy;
}
}
Program to Class diagram
Point
MultipleConstructor
- x: int
<<create>>
- y: int
+ main( String ) : void
+ Point( )
+ Point( int, int)
284
What do you notice? Objects!
285
What do you notice? Properties!
286
What do you notice? Behavior
287
And What else?
288
And What else?
289
Classes?
290
Classes
291
Classes
292
Attributes
293
Attributes
294
Attributes
295
Methods
296
Methods
297
Methods
298
Methods
299
Methods
300
Final Thoughts
301
Super class vs. Sub class
# protected attributes
- private attributes
+ public operations
# protected operations
- private operations
anAssociation aLink
306
Association
307
Multiplicity
Multiplicity specifies “the number of instances of one
class that may relate to a single instance of an
associated class”.
• “One” or “Many”
• Infinite (subset of the nonnegative integers)
308
Association
Visual representation
One-to-one association
309
Association
Many-to-one association
Person Company
OwnStock
name name
* *
Many-to-many association
310
Association
Interpretation?
Person and Company participate in association “WorkFor”
311
Association Class
UML offers the ability to describe links of association
with attributes like any class.
312
Association Class
313
Association Class vs Ordinary Class
Association Class
Ordinary Class
314
N-ary Association
Programmer
Class Diagram
315
Aggregation
Aggregation is a special type of Association that shows “part
of” relationship
Transitive
if B is part of A and C is part of B, C is also part of A
Asymmetric
it is not possible for A to be part of B and B to be part of A
simultaneously
A B C
A B
Include
Extend
Include:
“extension” use case extends the behaviour of the “base” use case
There can be several “extend” use cases for one “base” use case
B: Child class
+ x : int
12/15/2023 327
Super - keyword
Super keyword is used to refer parent class
entities
12/15/2023 328
Super – keyword - Rules
Syntax (from subclass, to access members of parent class)
Variables: super.VariableName
Methods: super.MethodName( )
Constructors: super(<parameter_list>)
12/15/2023 330
this keyword
this keyword is used to refer to the object which invokes the
method
this keyword helps to use same variable name for both local and
instance variables
Syntax
Variables: this.VariableName
Methods: this.MethodName( )
Constructors: this(<parameter_list>)
12/15/2023 331
this keyword
this keyword should be the first statement within a
constructor block
12/15/2023 332
this keyword
this.x = x;
12/15/2023 333
Memory Management - Objects
class ABC public class TEST
{ {
int x; psvm(String[] args)
void show( ) {
{ ABC a = new ABC( );
int x = 5; ABC b = new ABC( );
this.x = 10; System.out.println(a.x);
System.out.println(x); a.show( );
} System.out.println(b.x);
} }
}
OUTPUT: 0 5 0
12/15/2023 334
Re-defining variables in Java
Local variable
Instance variable
Class variable
Local variable:
Variables are accessible within the block where they are declared
Variables declared within a method/constructor are always accessible
from within the method/constructor.
Instance variable:
Variables that are declared within a class
Variables which are not within a method/constructor, but inside a class
Variables are associated with the object of the class
All instances or objects of the class gets separate copy or memory
location of the instance variables.
12/15/2023 335
Re-defining variables in Java
Instance methods:
Methods that are declared within a class
Methods are associated with the object of the class
All instances or objects of the class gets separate copy or memory
location of the instance methods.
Class variable:
Variables belongs truly within the class
All instances or objects of the class shares the same variable
Separate memory is not allocated for the class variables with respect to
the multiple objects of the class
Multiple instances or objects of the class do not get separate copy or
memory location of the class variables
Class variables are called Static variables
Methods that require similar property are called Static methods.
12/15/2023 336
Static keyword
Static keyword helps to keep single copy of a variable or method
within a class
Static attributes belongs to the class and not to any object of the
class.
12/15/2023 337
Static usage within Java Virtual Machine
Class Loaded
12/15/2023 338
Static keyword - Limitations
Overriding is not possible on static methods
Syntax:
12/15/2023 339
final keyword
final keyword is used to perform the following
final variables
final int x = 10;
A variable once declared FINAL, can never change its value
throughout the program.
12/15/2023 340
final methods
class T1
{
final void display( )
{
System.out.println(“Pramit”);
}
}
class T2 extends T1
{ Error. Final methods
void display( ) cannot be overridden
{
System.out.println(“Mazumdar”);
}
}
12/15/2023 341
final keyword
final methods cannot be overridden
final Class
final class can contain both final and non-final attributes
12/15/2023 342
final vs static keyword
static final
Static methods cannot be overridden. Final methods cannot be overridden.
Static classes can be inherited. Final classes can never be inherited.
Static keyword can never make a Final keyword makes a variable
variable constant. It is shared by all constant.
instances of objects and has a single
copy.
Static variable can change their value Final variable once declared can
during program execution. never be changed.
These are initialized when class Final variables are accessible after
loader loads the class. the object is created.
12/15/2023 343
Controlling Inheritance
Make a Class non-inheritable = Final
12/15/2023 344
Method Overriding - Example
class Bank{ class AXIS extends Bank{
int getRateOfInterest( ) int getRateOfInterest( )
{ return 0; } { return 9; }
} }
public class Method_Overriding{
class SBI extends Bank{ psvm(S a[]){
int getRateOfInterest( )
{ return 8; } SBI s=new SBI( );
} ICICI i=new ICICI( );
AXIS a=new AXIS( );
class ICICI extends Bank{ S.O.P(“SBI”+ s.getRateOfInterest( ));
int getRateOfInterest( ) S.O.P(“ICICI”+i.getRateOfInterest());
{ return 7; } S.O.P(“AXIS”+a.getRateOfInterest());
}
}
}
12/15/2023 345
Abstraction
An essential element of object-oriented programming is
abstraction
12/15/2023 346
Abstraction
Process of representing essential features without including the
background details or explanations
It does not contain any body or statement within it, i.e. they
are not defined within the class considered as abstract
12/15/2023 347
Abstract in Java
abstract class test5 public class check
{ {
void show( ) { public static void main(String args[])
System.out.println("Mazumdar"); {
} check1 C = new check1( );
C.display( );
abstract void display( ); C.show( );
} }
}
class check1 extends test5
{
void display( ) { We try not to define a
System.out.println("Pramit"); normal methods
inside an abstract
}
class
}
12/15/2023 348
Abstract in Java
Classes considered as abstract cannot have Objects
12/15/2023 349
Abstract in Java
Syntax:
abstract class test
{
……
}
12/15/2023 350
Abstract properties
Abstract methods cannot be static
12/15/2023 351
Abstract limitation
Abstract keyword may be used to perform abstraction in Java
12/15/2023 352
Interface
Multiple inheritance can be performed in Java using an
Interface
12/15/2023 353
Interface
Interface specifies what a class must do, but not how it does it
12/15/2023 354
Interface
Interface contains;
Variables which are static
Variables which are final
Methods which are Abstract
12/15/2023 355
Package
Packages are used to group a variety of classes and/or interfaces
together
12/15/2023 356
Package creation
Create a folder with same name as the desired Package name
Create a normal java file and write the class definition within it
Access specifiers provided for the class should agree with the
requirements
Place the java file in the same folder with the package name
12/15/2023 357
Package creation
Java files present in a package need not be compiled
Create a new java file. Outside the package folder. Within the same
path
Application program (java file) that uses a class (java file) present
within a package must IMPORT it.
12/15/2023 358
Package creation
Syntax;
import <package name> . <classname> ; //one class within the package folder
import <package name> . * ; //all classes within the package folder
Separate access specifiers for class and methods within the classes
12/15/2023 359
Elements of Object Models
Four major elements of the Object Model
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency
Persistence
12/15/2023 360
Concurrency
Concurrency is the property that distinguishes an active
object from one that is not active
Object 1 Object 2
Object 3
12/15/2023 361
Concurrency
Possible scenarios:
Object 1 is sending a message to Object 2 requesting for a service
12/15/2023 362
Concurrency
Allows multiple tasks to execute, interact, and collaborate at
the same time to achieve the global functionality
12/15/2023 363
Concurrency: Heavyweight and Lightweight
Heavyweight Concurrency:
Lightweight Concurrency:
12/15/2023 364
Process
class T1 Java code
{
public static void main(String args[])
{ Compile
int sum = 0;
12/15/2023 365
Process
Problem: Loop would run from 0 to 10000000 (10 million) times
The numbers after 5 million need to wait for execution until the
numbers before them are executed.
12/15/2023 366
Process
Java code Compile Interpret Executable
A program in
execution is Process
called Process
12/15/2023 367
Process
Only one processor is used
Time consuming
Solution:
Divide the task into parts
10 million can be divided into 4 parts (2.5 million each)
Each task can be assigned separate process
4 processes each with 2.5 million numbers
Each process assigned to separate processors
4 process executes on 4 processors
12/15/2023 368
Process
Java code Compile Interpret Executable
12/15/2023 369
Process
What we achieved:
Parallelization is achieved
Speed up the execution
Data
Heap
Stack, etc.
12/15/2023 370
Process
Interprocess communication (IPC) which communicates
among the processes in execution
12/15/2023 371
Process
Why creation of 4 separate process is a problem?
12/15/2023 372
Thread
Threads are extremely light weight when compared to
processes
Process
12/15/2023 374
Process
Java code Compile Interpret Executable
12/15/2023 375
Thread
Thread Process
Thread has no data segment or heap Process has code, heap, stack, and
to store/access data other segments as well
Thread cannot live on its own. It is A process can execute independently.
always associated with a process
There can be one of more threads Every process has at least one thread
associated with a process for execution
12/15/2023 376
Thread
A sequential program has only one single flow of
control
12/15/2023 377
Thread
A program is divided into a number of parts
12/15/2023 378
Thread
Process is an isolated execution entity which has its
own code, heap, stack, and other segments
Thread does not have its own data segment and heap
12/15/2023 379
Thread
A process cannot execute without a thread
12/15/2023 380
Thread
12/15/2023 381
Thread
class ABC
{
public static void main(String x[])
{
……
……
}
}
Single Thread
12/15/2023 382
Thread
Thread A
Switching
Switching
Thread C
Multithreading
12/15/2023 383
Life Cycle of a Thread
Ready for
New Born execution, but
CPU may waiting for
release a CPU
running start( )
allocation.
thread.
Ready
yield( ) run( )
Running
CPU is
stop( ) allocated and
is executing.
Dead
12/15/2023 384
Life Cycle of a Thread
1. NEW BORN
12/15/2023 385
Life Cycle of a Thread
2. READY or RUNNABLE
start( ) is the only method that can be used for starting a new
thread
12/15/2023 386
Life Cycle of a Thread
3. RUNNING
12/15/2023 387
Life Cycle of a Thread
4. DEAD
12/15/2023 388
Thread program in Java
Threads can be created in Java using;
12/15/2023 389
Thread program in Java
Runnable interface
Pass the object of the class along with the Thread object
12/15/2023 390
Thread programs
REFER PROGRAMS
thread_eg1.java
thread_eg2.java
12/15/2023 391
Life Cycle of a Thread
New Born
start( )
Ready
yield( ) run( )
Running
stop( )
Dead
12/15/2023 392
Life Cycle of a Thread
New Born
start( )
after t secs.
Ready
Wait
yield( ) run( )
Running
stop( )
Sleep
Dead
12/15/2023 393
Life Cycle of a Thread
sleep( ) [paused execution]
Java pauses the thread for a specified seconds (user defined)
Thread does not lose its ownership
Thread resumes execution after the specified time is over
CPU allocation is managed accordingly, so that after specified time it
can be brought back to running state
Failures of automatic transition from ready to running state may be
disrupted in a few extra-ordinary scenarios.
12/15/2023 394
Life Cycle of a Thread
New Born
start( )
Ready
Running
stop( )
Dead
12/15/2023 395
Life Cycle of a Thread
New Born
start( )
Ready
yield( ) run( )
Running
stop( )
Dead
12/15/2023 396
Life Cycle of a Thread
New Born
start( )
after t secs.
Ready
Wait
Blocked yield( ) run( )
Running
stop( )
Sleep
Dead
12/15/2023 397
Thread programs
REFER PROGRAMS
thread_eg3.java
thread_eg4.java
12/15/2023 398
Thread Priority
Priority to threads can be set
Syntax:
<ThreadName>.setPriority ( int Number )
12/15/2023 399
Thread programs
REFER PROGRAMS
thread_eg5.java
thread_eg6.java
12/15/2023 400
Synchronization
A thread may try to read a resource which is now been used by
another thread
This is due to the fact that both the threads are accessing the
same resource
Thus other methods are restricted from calling the same method.
12/15/2023 401
Synchronization
Therefore multiple threads may not access a single data/method at
the same time
Syntax:
synchronized void display( )
{
……
……
}
12/15/2023 402
Exception Handling
Any error during program execution may produce incorrect output
Types of errors;
Compile time error
Run time error
12/15/2023 403
Exception Handling
Run time errors:
Programs may be compiled correctly and class file could be created
However, the program may fail to execute
Alternatively it may produce wrong results due to wrong logic in the
programs
For example;
Dividing an integer by zero
Accessing an element which is out of an array limit, etc.
Exception
Refers to an unwanted or unexpected event
That may occur during the execution of a program (runtime)
Disruption in program flow may occur
If such an exception is not handled then an error message is displayed
And the program is terminated
12/15/2023 404
Exception Handling
ERROR EXCEPTION
12/15/2023 405
Exception Class Hierarchy
12/15/2023 406
Exception Handling
If we want to continue execution of the remaining code
12/15/2023 407
Exception Handling
TRY Exception
object creator
Statement that causes
exception
Throws exception object
CATCH
Exception handler
Statement that handles
the exception
12/15/2023 408
Exception Handling
Exception handling has two parts; TRY and CATCH
Searches for the correct catch block that handles the occurred
exception
12/15/2023 409
Exception Handling
Code written within the catch block is executed for handling the
exception
After execution, the control goes to the next line of the instruction
after the Catch block
12/15/2023 410
Exception Handling
try
{
……..
……..
}
catch(Exception_Type e)
{
…………
…………
System.out.println( e.getMessage );
}
12/15/2023 411
Exception Handling
try
{
……..
……..
}
catch(Exception e)
{
…………
…………
System.out.println( e.getMessage );
}
12/15/2023 412
Exception Handling
A try block can have multiple catch blocks
FINALLY BLOCK:
Instructions written within Finally block is bound to execute
12/15/2023 413
Exception Handling
Instructions which must be executed in a program might be kept
in the Finally block
Syntax:
try
{
……
}
catch(Exception e)
{
…..
}
finally
{
……
}
12/15/2023 414
Exception Handling
A message is generated by the JVM on encountering an exception
12/15/2023 415
Exception Handling
TRY Exception
object creator
Statement that causes
exception
Throws exception object
CATCH
Exception handler
Statement that handles
the exception
12/15/2023 416
Exception Handling
try
{
……..
……..
}
catch(Exception_Type e) {
…………
…………
System.out.println( e.getMessage );
}
catch(Exception e) {
…………
…………
System.out.println( e.getMessage );
}
12/15/2023 417
Throw Exception
public class exception_eg1 {
public static void main(String args[])
{
try
{
int x[] = new int[5];
x[20]=10;
System.out.println(“Pramit”);
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
} }
12/15/2023 418
Throw Exception
Usually exception is automatically identified and thrown by the
JVM
Syntax:
12/15/2023 419
Throw Exception
public class exception_eg2 {
public static void main(String args[ ]) {
try {
int x[ ] = new int[5];
x[4]=10;
12/15/2023 420
Throw User-defined Exception
User-defined exceptions can also be created
Syntax:
throw new ArrayIndexOutOfBoundsException ( “Here goes the Instruction” )
Therefore, as the first step we would create class for the user-defined
exception, that extends the Exception class
12/15/2023 421
Throw Exception
class my_exception extends throw new my_exception("\n You
Exception are approaching the Array Limit !!
{ \n");
my_exception(String msg)
{ }
super(msg); catch(my_exception e) {
} System.out.println(e.getMessage( ));
} }
} }
public class exception_eg2 {
public static void main(String
args[ ]) {
try {
12/15/2023 422
Throws Exception
Throw keyword can only be used if we want to handle an exception
from the same method
Usual Scenario:
12/15/2023 423
Throws Exception
New Scenario:
12/15/2023 424
Throw Exception
class test_throws try
{ {
void disp() throws T.disp( );
ArithmeticException, Exception }
{
int x = 7/0; catch(ArithmeticException e) {
}
} e.printStackTrace( );
12/15/2023 425
Elements of Object Models
Four major elements of the Object Model
Abstraction
Encapsulation
Modularity
Hierarchy
Typing
Concurrency
Persistence
12/15/2023 426
Persistence
Persistence is the property of an object through which its
existence transcends time
Once the creator no longer exists then how long will the
object stay or exist is an independent decision taken by OS
12/15/2023 427
Continuum of Object Existence
An object in software takes up some amount of space and exists for
a particular amount of time
12/15/2023 428
Continuum of Object Existence
Spectrum of object persistence: EXAMPLES
12/15/2023 429
Why Persistence is Important?
If an object is very transitive, i.e. it occurs and then
disappears then we would like to use a lightweight process
for the task/object
12/15/2023 430
Garbage Collection
JVM maintains a heap to store all objects created by a running
java application
12/15/2023 431
Garbage Collection
Garbage collector is a part of the JVM which significantly helps in
memory management related to Java programs
12/15/2023 432
Finalize
Finalize is a method present in java.lang.Object class
12/15/2023 433
Finalize
finalize( ) method can be used for this purpose
12/15/2023 434
Finalize
class test_finalize {
protected void finalize( ) {
System.out.println("Object Deleted !!");
}
}
System.gc( );
}
}
12/15/2023 435
Sequence Diagram
12/15/2023 436
Sequence Diagram
12/15/2023 437
Lifeline in a Sequence Diagram
12/15/2023 438
Objects and Lifeline
12/15/2023 439
Objects and Life Time
12/15/2023 440
Start and End of Life Line
12/15/2023 441
Messages in Sequence Diagram
12/15/2023 442
Synchronous and Asynchronous Messages
12/15/2023 443
Lost and Found Message
12/15/2023 444
Self or Recursive Message
12/15/2023 445
Controlled Message
12/15/2023 446
Duration and Timing Constraints
12/15/2023 447
UML: Sequence diagram
Actor Message Object Lifeline
button1Pressed
button1&2Pressed button2Pressed
Blink Increment
Minutes Minutes
button1Pressed
button2Pressed
Stop Blink Increment
Blinking Seconds Seconds
12/15/2023 449
State Machine Diagram _ States
12/15/2023 450
Transition _ Self Transition
12/15/2023 451
Compound State
12/15/2023 452
Entry _ Exit States
12/15/2023 453
Choice Pseudo State
12/15/2023 454
Junction Pseudo State
12/15/2023 455
Terminate Pseudo State
12/15/2023 456
Terminate Pseudo State
12/15/2023 457
History State
12/15/2023 458
Concurrent Regions within a State
12/15/2023 459
Concurrency in State Diagram
12/15/2023 460
UML: Statechart diagrams
The behavior of the single object Watch, for example, has several
different interesting states, BlinkHours, BlinkMinutes, BlinkSeconds,,
etc.
state
initial State
event
transition
When to develop a state chart?
Model objects that have change state in interesting ways:
Devices (microwave oven, Ipod)
Etc.
UML: Statechart diagrams
Event Initial state
button1&2Pressed button2Pressed
Blink Increment
Hours Hours
Transition button1Pressed
button1&2Pressed button2Pressed
Blink Increment
Minutes Minutes
State
button1Pressed
button2Pressed
Stop Blink Increment
Blinking Seconds Seconds
Final state
• Initial and Final States - The initial state is denoted by a filled black circle
and may be labeled with a name. The final state is denoted by a circle with
a dot inside and may also be labeled with a name.
State Machine Diagrams
• Transitions - Transitions from one state to the next are denoted
by lines with arrowheads. A transition may have a trigger, a guard
and an effect, as below.
• Self-Transitions - A state can have a transition that returns to
itself, as in the following diagram. This is most useful when an
effect is associated with the transition.
Alternative
way to show
the same
information
•The ∞ symbol indicates that details of the Check PIN
sub-machine are shown in a separate diagram.
State Machine Diagrams
Entry Point - Sometimes you won’t want to enter a sub-machine at
the normal initial state. For example, in the following sub-machine it
would be normal to begin in the "Initializing" state, but if for some
reason it wasn’t necessary to perform the initialization, it would be
possible to begin in the "Ready" state by transitioning to the named
entry point.
476
Thank You
12/15/2023 477