Professional Documents
Culture Documents
Basics of Object
Oriented Programming
Pramit Mazumdar
9/22/2021 1
Topics
Programming languages
Introduction to Java
Java execution
9/22/2021 2
Programming Languages
Monolytic programming language
9/22/2021 3
Programming Languages
Monolytic programming language
Sequential lines of code
Do not have sub-routine calls
A portion of code if needed to repeat, is duplicated
Example: BASIC
9/22/2021 4
Programming Languages
Structured/Modular programming language
9/22/2021 5
Programming Languages
Object oriented programming languages
9/22/2021 6
Programming Languages
Procedural & Sequential Object oriented programming
programming
Programs are divided into small parts Programs are divided into small parts
called Functions call Objects
Very limited code reuse facility Code reuse is a fundamental property
9/22/2021 7
Object Oriented Programming
OOP is an approach to program organization and
development
Inheritance
Polymorphism
Dynamic binding
9/22/2021 8
Object Oriented Programming
Re-usability
Reusing some facilities rather than building them again and
again
Data redundancy
9/22/2021 9
Object Oriented Programming
Code maintenance
Security
9/22/2021 10
Object Oriented Programming
Design benefits
Better productivity
9/22/2021 11
Object Oriented Programming
Troubleshooting
Polymorphism
9/22/2021 12
Pillars of OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
9/22/2021 13
Introduction to Java
Compiled and Interpreted
9/22/2021 14
Introduction to Java
Java virtual machine (JVM) stores the byte code
9/22/2021 15
Introduction to Java
JDK/J2SDK/JSE includes the following:
javac (compiler)
java (interpreter)
9/22/2021 16
Java execution
Javac
Text editor Source Code
compiler
javadoc
Program Java
interpreter
Java class file
output
javah
jdb
9/22/2021 17
Java program execution
Using J2SDK
Notepad -> program.java -> save in BIN folder of JDK -> open command
prompt -> javac program.java -> program.class created -> java program.c
-> java program executed.
Eclipse
Netbeans
9/22/2021 18
Java w.r.t to C++
No support for operator overloading
No concept of pointers
No destructor function
No header files.
9/22/2021 19
Thank You
9/22/2021 20
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
9/23/2021 1
Topics
Class – Methods – Objects
Scope of variables
9/23/2021 2
Class – Objects – Methods
Object oriented programs deal with Objects
9/23/2021 3
Class – Objects – Methods
Here we write codes in separate blocks
9/23/2021 4
Class – Objects – Methods
Method - 1 Method - 1 Method - 1
Method - 2 Method – 2
Method - 3
Class 1 Class 2
Class 3
9/23/2021 5
Class
Class is a structure that defines the data and
methods to work on
9/23/2021 6
Instance
Every element (variables and methods) declared
within a class does not allocate memory until an
object is declared
9/23/2021 7
Methods
Class consists of collection of methods
9/23/2021 8
Objects
Objects are instances of a class or an executable
copy of a class
9/23/2021 9
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.
9/23/2021 10
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.
9/23/2021 11
First program
class sample
{
void display( )
{
System.out.println(“Pramit”);
}
}
class test
{
public static void main(String args[])
{
sample SS = new sample( );
SS.display( );
}
}
9/23/2021 12
Object creation
sample SS;
SS = new sample( );
sample SS
Reference created
Storage space allocated
Storage space not yet instantiated
SS = new sample( )
Storage space instantiated
Sample class object created.
9/23/2021 13
Scope of variables
Instance variable
Class variable
Local variable
9/23/2021 14
Instance variable
These variables are declared inside a class
9/23/2021 15
Thank You
9/23/2021 16
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
9/27/2021 1
Scope of variables
Instance variable
Class variable
Local variable
9/27/2021 2
Instance variable
These variables are declared inside a class
9/27/2021 3
Class variable
Class variables exists among every instance or
object of a class
9/27/2021 4
Local variable
These variables are declared and used within a
particular method of a class
9/27/2021 5
Java variables – sample program
class test{
int x = 2; //class variable valid throughout the class
void display_1( ) {
s.o.p(“Pramit”);
int y = 50; //local variable, scope within method
x = 10; // class variable, so can be accessed from here
}
void display_2( ) {
s.o.p(“value of x is” + x);
s.o.p(“value of y is” + y); //error, y is local to method display_1( )
}
}
9/27/2021 6
Java variables – sample program
class program_variables {
p s v m(S a[ ]) {
9/27/2021 7
Identifying Objects and how they are
related?
8
Overview
Identifying Objects
– Identity
– Properties
– Behaviour
Identify Classes
– Name
– Class Attributes
– Class Methods
Identify Relationships
10
What do you notice?
Regular 2D shapes can be polygons or circles
12
What do you notice? Objects!
13
What do you notice? Properties!
14
What do you notice? Behavior
15
And What else?
16
And What else?
17
Classes?
18
Classes
19
Classes
20
Attributes
21
Thank You
9/27/2021 22
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
9/29/2021 1
Identifying Objects and how they are
related?
2
Overview
Identifying Objects
– Identity
– Properties
– Behaviour
Identify Classes
– Name
– Class Attributes
– Class Methods
Identify Relationships
4
What do you notice?
Regular 2D shapes can be polygons or circles
6
What do you notice? Objects!
7
What do you notice? Properties!
8
What do you notice? Behavior
9
And What else?
10
And What else?
11
Classes?
12
Classes
13
Classes
14
Attributes
15
Attributes
16
Attributes
17
Methods
18
Methods
19
Methods
20
Methods
21
Methods
22
Final Thoughts
23
Super class vs. Sub class
# protected attributes
- private attributes
+ public operations
# protected operations
- private operations
anAssociation aLink
29
Association
30
Thank You
9/29/2021 31
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
9/30/2021 1
Topics
Class diagram
Association
Aggregation
Composition
9/30/2021 2
Class Diagrams
A Class defines the attributes and the methods of a set of
objects.
# protected attributes
- private attributes
+ public operations
# protected operations
- private operations
anAssociation aLink
7
Association
8
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)
9
Association
Visual representation
One-to-one association
10
Association
Many-to-one association
Person Company
OwnStock
name name
* *
Many-to-many association
11
Association
Interpretation?
Person and Company participate in association “WorkFor”
12
Association Class
UML offers the ability to describe links of association
with attributes like any class.
13
Association Class
14
Association Class vs Ordinary Class
Association Class
Ordinary Class
15
N-ary Association
Programmer
Class Diagram
16
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
Shared aggregation
Informal semantics
expresses a weak belonging of the parts to a whole
parts also exist independently of the whole.
9/30/2021 21
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
10/3/2021 1
Initialization of Objects
class test { class test { class test {
...
}
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);
OUTPUT:
p1: (5, 2) p2: (4, 3) p2: (6, 7)
Multiple constructors
A class can have multiple constructors.
Each one must accept a unique set of parameters.
public Point() {
x = 0;
y = 0;
}
p2.translate(2, 4);
System.out.println("p2: (" + p2.x + ", " + p2.y + ")");
}
}
OUTPUT:
p1: (5, 2) p2: (4, 3) p2: (6, 7) p3: (0,0)
Thank You
10/3/2021 13
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
10/6/2021 1
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 + ")");
}
}
OUTPUT:
p1: (5, 2) p2: (4, 3) p2: (6, 7) p3: (0,0)
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
Two types:
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.
10/5/2021 10
CS 201
Polymorphism
Pramit Mazumdar
10/10/2021 1
Topics
Class diagram
Signature
Polymorphism
Types of Polymorphism
10/10/2021 2
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
- x: int MultipleConstructor
<<create>>
- y: int
+ main( String ) : void
+ Point( )
+ Point( int, int)
7
Polymorphism
This feature is known as Polymorphism
8
Polymorphism
Compile time polymorphism (Static/Early binding)
9
Overloading
class Test {
public static void main(String args[]) {
void myPrint(5);
void myPrint(5.0);
}
void myPrint(int i) {
System.out.println("int i = " + i);
}
void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
10
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.
11
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();
}
12
Thank You
10/10/2021 14
CS 201
Pramit Mazumdar
10/11/2021 1
Topics
Legal and Illegal Assignments
Operator Overloading
10/11/2021 2
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
}
}
Widening is legal
Narrowing is illegal (unless you cast)
3
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
4
Illegal method calls
class Test {
public static void main(String args[]) {
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println(i);
}
}
5
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
8
Operator Overloading
Overloading is of two types;
Method overloading (includes constructors)
Operator overloading
10/11/2021 10
Operator Overloading - Example
10/11/2021 11
Operator Overloading
Almost all operators can be overloaded
sizeof
member selector - .
ternary operator - ?:
10/11/2021 12
Operator Overloading - Syntax
10/11/2021 13
Operator Overloading - Restrictions
Precedence and Associativity of an operator
cannot be changed.
10/11/2021 14
/ 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 / t2;
void operator/(test); 5
return 0;
}; } 2
10/11/2021 15
<< 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;
}
10/11/2021 17
== 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;
}
10/11/2021 19
The software crisis
software engineering: The practice of developing, designing,
documenting, testing large computer programs.
10/11/2021 21
CS 201
Object Oriented
Programming Concepts
Pramit Mazumdar
10/13/2021 1
Topics
Why code reuse is necessary?
Method Overriding
10/13/2021 2
The software crisis
software engineering: The practice of developing, designing,
documenting, testing large computer programs.
rules
useful.
Is-a relationships, hierarchies
is-a relationship: A hierarchical connection where one
category can be treated as a specialized version of another.
every marketer is an employee
extended.
Subclass/derived class: The child class that extends the
Example:
3, i.e. 15 days).
Lawyers use a pink form when applying for vacation leave.
sue.
method.
Just write a new version of it in the subclass.
10/13/2021 19
CS 201
Inheritance & Access
Specifiers
Pramit Mazumdar
10/13/2021 1
Topics
Types of Inheritance
Access Specifiers
10/13/2021 2
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
10/13/2021 3
Single Inheritance
A child class derives member variables
and methods from only one parent
class.
A: Parent class
B: Child class
10/13/2021 4
Multiple Inheritance
A child class derives member variables
and methods from multiple parent
classes.
C: Child class
10/13/2021 5
Multilevel Inheritance
A child class derives member variables
and methods from another derived
class.
A: Parent class
B: Child class
C: Child class
10/13/2021 6
Hierarchical Inheritance
One base class is derived by many child
classes.
A: Parent class
10/13/2021 7
Hybrid Inheritance
Combination of two or more types of
inheritances.
A: Parent class
D: Child class
10/13/2021 8
Inheritance
Type of Inheritance C++ Java
Single Inheritance Yes Yes
Multiple Inheritance Yes No
Multilevel Inheritance Yes Yes
Hierarchical Inheritance Yes Yes
Hybrid Inheritance No No
10/13/2021 9
Single Inheritance
A child class derives member variables
and functions from only one parent
class.
A: Parent class
B: Child class
10/13/2021 10
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
10/13/2021 11
Single Inheritance – Class Diagram
One
- x: int
+ print_1( )
Single_Inheritance
+ main( ): void
Two
+ print_2( )
10/13/2021 12
Multilevel Inheritance
A child class derives member variables
and methods from another derived
class.
A: Parent class
B: Child class
C: Child class
10/13/2021 13
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);
} } }
10/13/2021 14
Multilevel Inheritance – Class Diagram
X
- i: int
+ X( )
Multilevel_Inheritance
Y
<<create>>
- j: int
+ Y( ) + main( ): void
Z
- k: int
+ Z( )
10/13/2021 15
Hierarchical Inheritance
One base class is derived by many child
classes.
A: Parent class
10/13/2021 16
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);
} } } }
10/13/2021 17
Multilevel Inheritance – Class Diagram
Shape
- i: int
+ Shape( )
square rectangle
- j: int - k: int
+ square( ) + rectangle( )
Multilevel_Inheritance
+ main( ): void
10/13/2021 18
Access Specifiers
Types of Access Specifiers:
Public
Default (Friendly)
Protected
Private
10/13/2021 19
Access Specifiers or Visibility Modes
Provides a restriction over the scope of a
variable/entity of a program
Class
Sub-class
Package
It is a group of similar types of classes, interfaces, etc.
10/13/2021 20
Public Access Specifier
Public attributes/methods are accessible from
anywhere in the program
10/13/2021 21
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.
10/13/2021 22
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.
10/13/2021 23
Private Access Specifier
WHO CAN ACCESS
1. All members within the same class
10/13/2021 25
Thank You
10/13/2021 26
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
10/19/2021 1
Topics
Method Overriding
Solutions in C++
10/19/2021 2
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)
10/19/2021 3
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());
}
}
}
10/19/2021 4
Method Overloading vs Overriding
Method Overloading Method Overriding
10/19/2021 5
Multiple Inheritance
A child class derives member variables
and functions from multiple parent
classes.
C: Child class
10/19/2021 6
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( );
}
10/19/2021 7
Hybrid Inheritance
Combination of two or more types of
inheritances.
A: Parent class
D: Child class
10/19/2021 8
Hybrid Inheritance – Problem
class A
public: int x
class D : public B, C
AMBIGUOUS int x
10/19/2021 9
Hybrid Inheritance – Solution in C++
Two ways to counter this problem:
10/19/2021 10
Hybrid Inheritance – Solution-1
class A
public: int x
class D : public B, C
B :: int x
10/19/2021 11
Virtual Base Class
class A
public: int x
class D : public B, C
int x
10/19/2021 12
Constructor calling - Inheritance
Base class constructor is called by default
10/19/2021 13
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
}
10/19/2021 14
Thank You
10/19/2021 15
CS 201
Object Oriented
Programming
Pramit Mazumdar
10/20/2021 1
How to call Variables, Methods, and
Constructors of Base class?
10/20/2021 2
Super - keyword
Super keyword is used to refer parent class
entities
10/20/2021 3
Super - keyword
Syntax (from subclass, to access members of parent class)
Variables: super.VariableName
Methods: super.MethodName( )
Constructors: super(<parameter_list>)
10/20/2021 4
Super – keyword - Rules
Syntax (from subclass, to access members of parent class)
Variables: super.VariableName
Methods: super.MethodName( )
Constructors: super(<parameter_list>)
10/20/2021 6
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
10/25/2021 1
Topics
this keyword
Program examples
10/25/2021 2
this keyword
this keyword can be used to call constructor from within
another constructor
10/25/2021 3
this keyword
this.x = x;
10/25/2021 4
Thank You
10/25/2021 5
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
10/26/2021 1
Topics
static keyword
10/26/2021 2
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.
10/26/2021 3
Static keyword - Limitations
Static methods can only access static variables
Syntax:
static int x = 10;
static int display(int x);
10/26/2021 5
Thank You
10/26/2021 6
CS 201
Basics of Object
Oriented Programming
Pramit Mazumdar
10/27/2021 1
Topics
final keyword
10/27/2021 2
final keyword
final keyword is used to perform the following
final variables
10/27/2021 3
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”);
}
}
10/27/2021 4
final keyword
final methods cannot be overridden
final Class
final class can contain both final and non-final attributes
10/27/2021 5
final vs static keyword
static final
Static methods can 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.
10/27/2021 6
Thank You
10/27/2021 7
CS 201
Abstract
Pramit Mazumdar
10/28/2021 1
Abstraction
An essential element of object-oriented programming is
abstraction
10/28/2021 2
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
10/28/2021 3
Abstract in Java
Classes considered as abstract cannot have Objects
10/28/2021 4
Abstract in Java
Syntax:
abstract class test
{
……
}
10/28/2021 5
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
}
10/28/2021 6
Abstract properties
Static methods cannot be abstract
10/28/2021 7
Abstract limitation
Abstract keyword may be used to perform abstraction in Java
10/28/2021 8
Thank You
10/28/2021 9
CS 201
Interface
Pramit Mazumdar
11/1/2021 1
Abstract limitation
Abstract keyword may be used to perform abstraction in Java
11/1/2021 2
Interface
Multiple inheritance can be performed in Java using Interfaces
11/1/2021 3
Interface
Interface specifies what a class must do, but not how it does it
11/1/2021 4
Interface
Interface contains;
Variables which are static
Variables which are final
Methods which are Abstract
11/1/2021 5
Thank You
11/1/2021 6
CS 201
Packages
Pramit Mazumdar
11/17/2021 1
Package
Packages are used to group a variety of classes and/or interfaces
together
11/17/2021 2
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
11/17/2021 3
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.
11/17/2021 4
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
11/17/2021 5
Thank You
11/17/2021 6
CS 201
Threads
Pramit Mazumdar
11/22/2021 1
Thread
class T1 Java code
{
public static void main(String args[])
{ Compile
int sum = 0;
11/22/2021 2
Thread
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.
11/22/2021 3
Thread
Java code Compile Interpret Executable
A program in
execution is Process
called Process
11/22/2021 4
Thread
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
11/22/2021 5
Thread
Java code Compile Interpret Executable
11/22/2021 6
Thread
What we achieved:
Parallelization is achieved
Speed up the execution
Data
Heap
Stack, etc.
11/22/2021 7
Thread
Interprocess communication (IPC) which communicates
among the processes in execution
11/22/2021 8
Thread
Why creation of 4 separate process is a problem?
11/22/2021 9
Thread
Threads are extremely light weight when compared to
processes
Process
11/22/2021 11
Thank You
11/22/2021 12
CS 201
Threads
Pramit Mazumdar
11/24/2021 1
Thread
Thread Process
Thread has no data segment or heap Process has code, head, 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
11/24/2021 2
Thread
A sequential program has only one single flow of
control
11/24/2021 3
Thread
A program is divided into a number of parts
11/24/2021 4
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
11/24/2021 5
Thread
A process cannot execute without a thread
11/24/2021 6
Thread
11/24/2021 7
Thread
class ABC
{
public static void main(String x[])
{
……
……
}
}
Single Thread
11/24/2021 8
Thread
Thread A
Switching
Switching
Thread C
Multithreading
11/24/2021 9
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
11/24/2021 10
Thank You
11/24/2021 11
CS 201
Threads
Pramit Mazumdar
11/25/2021 1
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
11/25/2021 2
Life Cycle of a Thread
1. NEW BORN
11/25/2021 3
Life Cycle of a Thread
1. READY or RUNNABLE
start( ) is the only method that can be used for starting a new
thread
11/25/2021 4
Life Cycle of a Thread
3. RUNNING
11/25/2021 5
Life Cycle of a Thread
4. DEAD
11/25/2021 6
Thread program in Java
Threads can be created in Java using;
11/25/2021 7
Thread program in Java
Runnable interface
Pass the object of the class along with the Thread object
11/25/2021 8
Thank You
11/25/2021 9
Introduction to UML
• UML is an abbreviation of Unified Modelling Language
• UML is a language
L (A ,R ) C o m m u n ic a t io n
la n g u a g e a lp h a b e ts g ra m m a r
– Visualizing
– Specifying
– Constructing
– Documenting
– Things
• Things are abstractions in a model
– Relationships
• Relationships tie the things together
– Diagrams
• Diagrams group relevant collection of things
Basic Building Blocks: Things
• There are four kinds of things in the UML
1. Structural things
2. Behavioral things
3. Grouping things
4. Annotational things
Structural Things in UML
• Relevant things that are either conceptual or physical
Examples: class, object, etc.
N am e
A t t r ib u t e s
O p e r a t io n s
Structural Things: Active Class
•Active Class
– An active class is just like a class
expect that its object represent
element whose behavior is
concurrent with other elements E vent M anger
A t t r ib u te s
– An object of an active class owns one
or more processes or threads and re s u m e ()
therefore initiate control activity s ta rt()
s u s p e n d ()
k ill( )
– Graphically, an active class is f lu s h ( )
denoted just like a class, but with a
heavy line, usually including its
name, attributes, and operations
Structural Things: Component
• Component
– Component of a system is to
conform with the realization of a set
of interfaces
m y P a c k a g e .h tm
– Example: An applet, com+
component, Java bean or ASP file.
In fact, a component is typically
represents a physical package of
otherwise logical elements such as
classes, interfaces and collaborations
–
iS y n o n
C om ponent
Graphically, an interface rendered as
iC o u n t
ym s
Example:
W o r ld T r a d e C e n te r
A big building (world trade center) is a
collaboration of some structural
construction, electrical wiring system,
telephone connectivity, internet
connectivity, water supply system, etc.
a s s ig n ( t e s tin g )
e : P e rs o n c : Com pany
Behavioral Things: State Machine
• State machine
– Using an interaction one can model the behavior of
a society of objects that work together
w a it r u n n in g
k e y p re s s
A s ta te m a c h in e fo r a n o b je c t
B u tto n
G r a p h ic s
Annotational Things in UML
• Annotational things are the explanation parts of UML
models
S im p le T e x t
U R L + T ext
IS D IT 6 0 1 0 5 # 2 0
C o m m e n ts
w it h im a g e s
Relationships in UML
• A relationship is a concern among things
V id e o C lip
p la y ( c : C lip )
s ta rt() I n d e p e n d e n t c la s s
s to p ()
r e s iz e ( )
re s e t()]
: H e re , if th e re is a c h a n g e in th e
c la s s C lip , th e o p e ra tio n p la y () in
D e p e n d e n t c la s s th e c la s s V id e o w ill b e e ffe c te d
Relationships: Association
• Association
1 ..* W o rk s fo r
P e rs o n * C om pany
e m p lo y e e e m p lo y e r
A s s o c ia tio n
1 Has
U n iv e r s ity * D e p a rtm e n t
A g g r e g a tio n
Relationships: Generalization
• Generalization
– A generalization is a generalized/specialized relationship in
which objects of the specialized elements (the child), are
suitable for objects of the generalized elements (the parent)
P a re n t
C h ild
Relationships: Realization
• Realization
– A realization is a semantic connection
• between two things: interface, class, component
• between use-case and collaboration
etc.
IE E E M e m b e r S tu d e n t
: :
b io d a ta ( ) b io d a ta ( )
: :
Diagrams in UML
• Any system can be viewed with five interrelated views
S tr u c tu r a l v ie w B e h a v io r a l v ie w
U s e r 's v ie w
Im p le m e n ta tio n v ie w E n v ir o n m e n ta l v ie w
Diagrams in UML
• To represent each view, UML provides nine diagrams,
popularly termed as UML diagrams or UML artifacts
S tru c tu ra l v ie w B e h a v io ra l v ie w
C la s s d a ig r a m s A c t iv it y d a ig r a m s
O b je c t d ia g r a m s I n t e r a c t io n o v e r v ie w d ia g r a m s
P a c k a g e d ia g r a m s S e q u e n c e d ia g r a m s
C o m p o s it e s t r u c t u r e d ia g r a m s C o lla b o r a t io n d ia g r a m s
S t a t e c h a r t d ia g r a m s
T im in g d ia g r a m s
U s e r 's v i e w
U s e c a s e d a ig r a m s
Im p le m e n ta tio n v ie w E n v iro n m e n ta l v ie w
C o m p o n e n t d a ig r a m s D e p lo y m e n t d a ig r a m s
What is a Use Case Diagram?
• The use case diagram is a diagram to model the use case view
of a system. The behavior of the system under development
(i.e. what functionality must be provided by the system) is
documented in a use case diagram
• Example:
– On-line purchase (OLP) system
On-Line Purchase (OLP) System
• Intended functions • Its surroundings
– Search items – Customer
– Place order – Manager
– Process order – Payment processor
– Control inventory
– Help on-line
Use Case Diagram of OLP System
S e a rc h ite m s
P la c e o rd e r
C u s to m e r
< < S y s te m > >
P ro c e ss o rd e r
P a y m e n t p ro c e s s o r
In v e n to ry c o n tro l
O n -lin e h e lp M anager
Usage of Use Case Diagrams
1. Use case diagram depicts the
desirable functionalities (dynamic
aspects) of an information system
S e a rc h ite m s
decomposition) of a system
In v e n to ry c o n tro l
• Formal definition
• A use case is a sequence of transactions performed by the system
that yields a measurable result of values for a particular user (actor)
P la c e o rd e r
Actor in Use Case Diagram
• Actor
– An actor is a person, organization, or external system that plays a role
in one or more interaction with the system
• Note: Actors are not part of the system; they represent any one or any thing
that interact with the system
Notation
P la c e o rd e r
In v e n to ry c o n tro l
Notation R e le a s e 2
M anager
T a x c a lc u la tio n
R e le a s e 3
O n -lin e h e lp
R e le a s e 4
Packages in Use Case Diagram
• Packages
– Packages enable to organize model elements into groups
Notation
Packages are depicted as a file folders and can be used on any
of the UML diagrams, including both use case diagrams, class
diagrams etc.
R e g is tr a tio n
Note: Usually package can be used when a UML diagram is quite large and
can not be accommodated on a single page, or organize a large diagram
into smaller ones
Packages in Use Case Diagram
• Example
S tu d e n t R e g is tr a t io n
S y s te m
C o u rs e
R e g is tr a t io n B illin g
a llo t m e n t
Scenario and Use Case
• Scenario
– A scenario is a sequence of steps describing an
interaction between a user and system
Note: From one scenario, another scenario can arise. For example: at 3 incomplete
information, at 6 authorization fails etc.
How to Create a Use Case Diagram?
• Three steps
1. Identify all actors
2. Identify all use cases
3. All associations between actors and use cases
….. etc.
Identifying all Use Cases
• The following questions may be used to help identify the actors for a
system
– What are the functional requirements?
– Will any actor create, change, store, remove, or read information in the
system?
….etc.
Identifying all Associations
• There will be an association between an actor and a use
case, if there is
– information storing/updating
….etc.
Use Case Relationships
• In addition to association relationship ( which may exist
between an actor and use-case) to represents a communication
between an actor and a use-case, there are other three types of
relationships may exist between use-cases
– Include
– Extend
– Generalization
Include Relationship
• A use case in UML is same as the function in
structured design
– Include
• To represent that a use case is composed of (reuse)
Include Relationship
• Further
– Multiple use cases may share a piece of some
common functionality. This functionality is placed
in a separate use case rather than documenting it in
every use case that needs it
e>> S e a rc h B o o k
clud
<<in
Is s u e B o o k
U p d a te B o o k
Extend Relationship
– The extended relationship is used to describe a variation on
normal behavior and one wish to use the more controlled
form. Usually extended relationship is considered to depict
the following situations
• Optional behavior
B illin g <<ex
tend> > C heque paym ent
>
c lu de>
<<in
O th e r p a y m e n ts <<in
c lude
>>
C r e d it c a r d p a y m e n t
Example: Extend Relationship
G e t R e g is tr a t io n
S e c u r it y C h e c k
P ro c e s s O rd e r
I f a s t u d e n t is
w ith f o r ie g n v is a
< < e x te n d > > < < e x te n d > >
C r e d it C a r d V a lid a t io n P a y R e b a te
If th e c u s to m e r
T o a s p e c ia l t y p e
is n o t a r e g u la r
o f c u s to m e r
c u s to m e r
Generalization Relationship
• Generalization relationship is very similar to the inheritance
relationship as in classes
Pay Fees
C r e d it C a r d D e m a n d D ra ft e B a n k in g
Generalization Relationship
• The generalization relationship can also be extended to actor
S tu d e n t
S tu d e n t
In t e r n a tio n
s tu d e n t
UG PG R e s e ra c h
Use Case Packaging
• Packaging is the mechanism in UML to manage complexity
• When there are too many use cases in the use case diagram, it
is better to package the related use cases so that it can be better
documented
Example: Use Case Packaging
U se case 1 U se case 2
U se case 3
U s e c a s e 3 .1 U s e c a s e 3 .2
U s e c a s e 3 .3
U s e c a s e 3 .3 .1 U s e c a s e 3 .3 .2
U s e c a s e 3 .3 .3
What is a Class Diagram?
• A class is a structural component of any object oriented system. The class
diagram used to model the structural view of the underlying system
• Class diagrams also show the attributes and operations of a class. Also it
can depict the navigability (direction of trace ability) and the constraints
that apply to the way objects are connected
Detail Class Diagram
• Class diagram also shows the attributes and operations of classes
B ook
Nam e
b o o k N a m e : S t r in g
Attributes
A u t h o r s : S t r in g
A ttr ib u te s A c c n N o :S t r in g
d a te O fIs u u e : D a te
is s u e T o :P e r s o n
d a te O fR e tu rn : D a te
Operations
O p e r a tio n s
is s u e ( b o r r o w e r I d : P e r s o n I D )
r e t u r n ( ) :flo a t
r e s e r v e ( b o r r o w e r I D :P e r s o n ,p r io r it y : in t )
e d it B o o k ( )
rem o v eB o o k ()
• Think: For the class Server, what are the attributes? operations?
Visibility
• The notation that precedes the attribute or operation name indicates the
visibility of the element, if the + symbol is used the attribute or operation has
a public level of visibility, if a - symbol is used the attribute or operation is
private. In addition the # symbol allows an operation or attribute to be defined
as protected and the ~ symbol indicates package visibility.
Relation Association
• An association implies two model elements have a relationship - usually
implemented as an instance variable in one class. This connector may include
named roles at each end, cardinality, direction and constraints. Association is the
general relationship type between elements. For more than two elements, a diagonal
representation toolbox element can be used as well. When code is generated for
class diagrams, associations become instance variables in the target class.
A s s o c ia tio n n a m e
C la s s A C la s s B
R o le A R o le B
Aggregation and Composition
A g g r e g a tio n n a m e
C la s s A C la s s B
C o m p o s itio n n a m e
C la s s A C la s s B
Association Class
A s s o c ia tio n n a m e
C la s s A C la s s B
A ssocN am e
W o rk s fo r
C la s s A C la s s B
E m p lo y e e D e p a rtm e n t
A ssocN am e
W o rk s
Multiplicity
cc
1
C la s s E x a c tly o n e
*
C la s s M a n y (z ero o r m o re }
0 ..1
C la s s O p tio n a l(z e ro o r o n e )
1 ..*
C la s s O n e o r m o re
Multiplicity & Association
Example of a Class Diagram
has
IIT D e p a rtm e n t
1 ..*
1
1 ..* 1 ..* 1 ..*
chairs
offers
works in
enrolled in
* 1 ..* 0 ..1 H O D
a tte n d s te a c h e s *
S tu d e n t C o u rs e F a c u lty
* * * 1 ..*
Another Example
Example: Internet
• ? Internet
– Network of networks (is a collection of servers)
– A collection of web pages
* lin k s 1
W eb page S erv er
*
has
1
N e tw o rk
• This class diagram shows a collection of all possible classes in the system
namely, the Internet and relationships among them
C la s s S
C la s s A C la s s B C la s s C
C la s s X C la s s Y
Another view of Internet Class Diagram
IIS A pache
* lin k s 1
W eb page S e rv e r
*
has
1
S ta tic D y a n m ic N e tw o rk
.a s p .js p .p h p LA N M A N W A N
* 1
O rd e r C u s to m e r
1
P e rso n C o rp o ra te
*
1 0 ..1
1 *
Ite m L is t P ro d u ct E m p lo y e e
What is an Object Diagram
• An object is an instance of a class
p u rc h a s e 1
O rd e r * C u s to m e r
1
P e rso n C o rp o ra te
*
0 ..1
* 1
Ite m L is t * P ro d u ct E m p lo y e e
• From the application point of views, the customer class signifies that a customer is
either a person or an organization or an employee of an organization
Example: Object Diagram
• Further consider a particular snapshot at an instance
ram: Person C u s to m e r
sita: Person
bhim: Employee
arjun: Employee P e rso n C o rp o ra te
oldIndia: Corporate [r a m , s it a ] [o ld I n d ia , n e w In d ia ]
newIndia: Corporate
E m p lo y e e
[b h im , a r ju n ]
Further assume that bhim and arjun are the employees of oldIndia
Example: Object Diagram
• The object diagram for the above snapshot will look like this
o ld In d ia : C o rp o ra te n e w In d ia : C o rp o ra te
b h im : E m p lo y e e a rju n : E m p lo y e e
s ita : P e rs o n
ra m : P e rso n
More Example: Object Diagram
• Let’s consider a small modification in the class diagram
p u rc h a s e fo r
C u s to m e r *
1
P erso n C o rp o ra te
E m p lo y e e
• This signifies that a customer being a corporate can purchase for others,
say person, organizations
More Example: Object Diagram
• A snapshot under this modified situation is
ram: Person
C u s to m e r
sita: Person
bhim: Employee
arjun: Employee 1
P e rso n C o rp o ra te
oldIndia: Corporate
[r a m , s ita ] [in d ia , o ld In d ia , n e w In d ia ]
newIndia: Corporate
india: Corporate E m p lo y e e
[b h im , a r ju n ]
O ld In d ia : C o rp o ra te N e w In d ia : C o rp o ra te
b h im : E m p lo y e e a rju n : E m p lo y e e
s ita : P e rs o n
ra m : P erso n
Interaction Diagrams
• Interaction diagrams model how groups of objects collaborate
in some behavior
– Collaboration diagrams
What is a Sequence Diagram?
• A sequence diagram shows object interactions arranged in
time sequence
• The objects participating in the interaction are shown at the top of the chart
as boxes attached to a vertical-dashed line
• Inside the box the name of the object is written with a colon separating it
form the name of the class and both the name of the class and object are
underlined
• Some times an anonymous object (only class name and underlined) is also
used
Life Line in a Sequence Diagram
• A lifeline represents an individual participant in a sequence diagram. A
lifeline will usually have a rectangle containing its object name. If its name
is self then that indicates that the lifeline represents the classifier which
owns the sequence diagram
Life Line in a Sequence Diagram
• Sometimes a sequence diagram will have a lifeline with an actor element
symbol at its head. This will usually be the case if the sequence diagram is
owned by a use case. Boundary, control and entity elements form
robustness diagrams can also own lifelines
Example: Objects and Life Line
m ih ir: S tu d e n t a u tu m n 0 6 : C o u rs e M g r : C o u rse
c la s s n a m e
a n o n y m o u s o b je c t
o b je c t n a m e
v e r t i c a l - d a s h e d l i n e s a t t a c h e d t o o b je c t s
Objects and Life Time
• The objects appearing at the top signifying that the object already existed
when the use case execution was executed.
• However, if some object is created during the execution of the use case
and participates in the interaction, then that object should be shown at the
appropriate place on the diagram where it was created
• The vertical dashed line in the sequence diagram is called the object’s life
time. The life time indicates the existence of the object at any particular
point of time
• A rectangle is used on the life time to indicate the activation symbol and
implies that the object is active as long as the rectangle exists
Example: Object’s Life Time
m ih ir: S tu d e n t
o b je c t s t a r t s a n
a c tiv ity h e r e
a n o b je c t a p p e a r s
h e re
o b je c t f i n i s h e s a n
a c tiv ity h e r e
L ife tim e
a u tu m n 0 6 : C o u rs e M g r
o b je c t e x p i r e s h e r e
Start and End of Life Line
• A lifeline may be created or destroyed during the timescale represented by
a sequence diagram. In the latter case, the lifeline is terminated by a stop
symbol, represented as a cross. In the former case, the symbol at the head
of the lifeline is shown at a lower level down the page than the symbol of
the object that caused the creation. The following diagram shows an object
being created and destroyed
Messages in Sequence Diagrams
• Two objects in a sequence diagram interacts with passing
messages between them
m ih ir: S tu d e n t a u tu m n 0 6 : C o u rs e M g r
1
r e q u e s tE n r o ll( )
2
s e a rc h ()
c o n fir m E n r o ll( )
T h r e e m e s s a g e s a re in
3 c h r o n o lg ic a l s e q u e n c e
T w o o b je c t a r e c o m m u n i c a t i n g
b y p a s s in g m e s s a g e s
More on Messages
• Messages can be synchronous or asynchronous; call or signal. In the
following diagram, the first message is a synchronous message (denoted by
the solid arrowhead) complete with an implicit return message; the second
message is asynchronous (denoted by line arrowhead) and the third is the
asynchronous return message (denoted by the dashed line)
More on Messages
• Messages can be lost or found. Lost messages are those that are either sent
but do not arrive at the intended recipient, or which go to a recipient not
shown on the current diagram. Found messages are those that arrive from
an unknown sender, or from a sender not shown on the current diagram.
They are denoted going to or coming from an endpoint element
Self or Recursive Messages
• A self message can represent a recursive call of an operation, or one
method calling another method belonging to the same object. It is shown as
creating a nested focus of control in the lifeline’s execution occurrence
Controlled Messages
m ih ir: S tu d e n t a u tu m n 0 6 : C o u rs e M g r : C o u rse
r e q u e s tE n r o ll( )
s e a rc h ()
[v a c a n t] c o n fir m E n r o ll( )
g e tC o u rs e ()
* g e tC o u rs e ()
s e n d th is m e s s a g e if th is
c o n d itio n is tr u e s e n d m e s s a g e s to a ll c o u r s e
o b je c t s
Duration and Timing Constraints
• By default, a message is shown as a horizontal line. Since the lifeline
represents the passage of time down the screen, when modeling a real-time
system, or even a time-bound business process, it can be important to
consider the length of time it takes to perform actions. By setting a
duration constraint for a message, the message will be shown as a sloping
line
ATM PIN Validation
a : c a rd R e a d e r b : s e s s io n M g r c : d is p la y M g r d : keyR eader e : aBank
c a rd In fo ()
b e g in
s e s s io n
[ ! v a lid A T M c a r d ]
e je c t ( )
ch e c k C a rd ()
sta tu s
[ s t a t u s .is S t o le n ]
r e t a in ( )
[ s t a t u s .c lo s e A c c o u n t ]
e je c t ( )
[ ! v a lid P I N & & t r y < 4 ]
re q u e stP IN ()
re a d P IN ()
v a lu e P I N
v e r ify P I N ( )
[ ! v a lid P I N ]
e je c t ( ) x x
d is p la y H e llo ( )
x x
x
Use Case Registration of OLP
Use case: Registration
b e g in s e s s io n re g n R e q u e st()
g e tC u sto m e rT y p e ()
in p u t T y p e ( )
C u sto m e rT y p e = re a d D a ta ()
[C u s to m e r T y p e = s ta ff]
g e tS ta ffD a ta ()
in p u t S t a ffD a t a ( )
c d = s ta ffC u s to m e rD a ta ()
s ta tu s 1 = c h e c k S ta ff(c d )
[ s t a t u s 1 = in v a lid ]
d is p la y M s g ( R e g n . fa il)
[ s t a t u s 1 = v a lid ]
[ s t a t u s 2 = e x is t ] s t a t u s 2 = c h e c k E x is t ( c d )
d is p la y M s g ( R e g n . f a il)
[ s t a t u s 2 = n o t E x is t ]
d is p la y M s g ( R e g n . s u c c e s s ) c 1 = c re a te C u sto m e r(c d )
u p d a te R e c o rd (c 1 )
[C u sto m e rT y p e = o th e r]
in p u t O t h e r D a t a ( ) g e tO th e rD a ta ()
c d = o th e rC u sto m e rD a ta ()
s t a t u s 2 = c h e c k E x is t ( c d )
[ s t a t u s 2 = e x is t ]
d is p la y M s g ( R e g n . f a il) [ s t a t u s 2 = n o t E x is t ]
c 2 = c re a te C u sto m e r(c d )
d is p la y M s g ( R e g n . s u c c e s s )
u p d a te R e c o rd (c 2 )
Use Case Place Order in OLP
Use case: Order Items
• The “Process Order” use case is proposed to have a following behavior (or
scenario)
• orderEntry: Window
– this object will get an order from a customer
• anOrder: Order
– receives an order from a customer (via orderEntry object)
• orderSet: ItemList
– an object is a list of items is to be processed
Sequence Diagram in OLP System
• stockist: InventoryManage
– object responsible for checking stock, supply stock, request for
inventory etc.
• :OrderInfo
– containing the orders information in a queue
• confirmMessage: Message
– message objects for sending confirmation message
• The sequence diagram for Process Order use case can be drawn as follows.
Sequence Diagram of Process Order use case
in OLP System
o rd e rE n try : C u s to m e r a n O rd e r: O rd e r o rd e rS e t: Ite m L is t s to c k is t: In v e n to ry M a n a g e r : O rd e rIn fo
c re a te () * s e tO rd e r()
s ta tu s = in v e n to r y C h e c k ()
[s ta tu s = F A L S E ]
e n q u e u e O rd e r()
a v a ila b le =
c h e c k S u p p ly ( )
[a v a ila b le = T R U E ] d e Q u e u e ()
r e p ly E s tim a te ( )
re p ly E s tim a te ( ) e s tim a te O rd e r()
a c c e p t()
c o n firm O rd e r:M e s s a g e
a c c e p t()
c o n fir m ( )
c o n firm ()
Use of Sequence Diagram
• From the sequence diagram of “Process Order” use case, it is evident that
the diagram is easy to understand and has immediate appeal. This is the
great advantage of the sequence diagram
– Messages shown as text and an arrow that points from a client object to
a respondent object
a n O rd e r:O rd e r
2 : * [fo r a ll ite m in O r d e r ]
s e tO rd e r()
3 : s ta tu s = in v e n to r y C h e c k ( )
o rd e rS e t:Ite m L is t 4 : a v a ila b le = c h e c k S u p p ly ( )
:In v e n to ry M a n a g e r
[s ta tu s ]
new [a v a ila b le ]
new
:C o n firm O rd e r :O rd e rIn fo
Note: Collaboration Diagram
• In collaboration diagram, the sequence indicated by
numbering the messages
– Focus of control
• In sequence diagram, there is the focus of control to show the
period of time during which object is performing an action. There
is no focus of control in collaboration diagram
Sequence Diagram vs. Collaboration Diagrams
• UML activity diagrams are the object oriented equivalent of flow chart and
data flow diagrams in function-oriented design approach
S y s te m
IIT
E n ro llm e n t
S tu d e n t R e g is t r a r
< < in c lu d e > >
C o u rs e re g is tra tio n
SEIIT System
• Here different activities are:
– Received enrollment form filled by the student
• Registrar checks the form
• Input data to the system
• System authenticate the environment
– Pay fees by the student
• Registrar checks the amount to be remitted and prepare a bill
• System acknowledge fee receipts and print receipt
– Hostel allotment
• Allot hostel
• Receive hostel charge
• Allot room
– Medical check up
• Create hostel record
• Conduct medical bill
• Enter record
– Issue library card
– Issue identity card
Activity Diagram for the Use Case in
SEIIT
R e c e iv e d f o r m
H o s te l
P a y m e n t fe e s M e d ic a l c h e c k
a llo t m e n t
Is s u e id e n tit y I s s u e lib r a r y
c a rd c a rd
Basic Components in an Activity Diagram
• Initial node
– The filled circle is the starting point
of the diagram
R e c e iv e d f o r m
• Final node
– The filled circle with a boarder is the
ending point. An activity diagram
can have zero or more activity final P a y m e n t f e e s H o s te l
M e d ic a l c h e c k
state. a llo t m e n t
• Activity
– The rounded circle represents
activities that occur. An activity is I s s u e id e n t it y I s s u e lib r a r y
not necessarily a program, it may be c a rd c a rd
– A merge passes any control flows straight through it. If two or more
inflows are received by a merge symbol, the action pointed to by its
outflow is executed two or more times
Basic Components in an Activity Diagram
• Decision
– A diamond with one flow
entering and several leaving. The
flow leaving includes conditions
as yes/ no state
• Flow final
– The circle with X though it. This
indicates that Process stop at this
point
• Swim lane R e c e iv e d f o r m
be horizontal or vertical
Detailed Activity Diagram of SEIIT
1
R e c e iv e d f o r m
H o s te l
P a y m e n t fe e s M e d ic a l c h e c k
a llo tm e n t
2
I s s u e id e n t it y Is s u e lib r a r y
c a rd c a rd
Detailed Activity Diagram of SEIIT
F ill-in C heck
fo rm fo rm
[In c o r re c t]
R e g re t m e ssa g e
[C o rre c t]
D is p la y s tu d e n t [N o t F o u n d ]
sc re e n
V e rify th e [F o u n d ]
In p u t s tu d e n t a p p lic a tio n s
in fo rm a tio n
C re a te re c o rd 1
S e a rc h fo r S tu d e n t [M a tc h ]
s e le c tio n lis t [N o M a tc h ]
R e g re t
re g is tra tio n
Detailed Activity Diagram of SEIIT
D is p la y C a lc u la te
1 e n ro llm e n t f o rm re g is tra tio n fe e s
D is p a ly fe e s
p a y a b le s c re e n
P ro c e ss
paym ent
P a y fee 2
P rin t re c e ip t
Activity Diagram of SEIIT with Swim Lane
Student
F ill- in
fo rm
V e rify th e [F o u n d ]
[In c o r re c t]
a p p lic a tio n s
[N o t F o u n d ]
C heck
R e g ret m e ssa g e
Registrar
fo rm
[C o rr e c t]
D is p la y s tu d e n t
sc ree n
C re a te re c o rd
System
In p u t s tu d e n t
in fo rm a tio n
[M a tc h ]
1
S e a rc h fo r S tu d e n t
s e le c tio n lis t [N o M a tc h ]
R e g ret
re g is tra tio n
Object and Object Flow
• An object flow is a path along which objects can pass. An
object is shown as a rectangle
• An object flow is shown as a connector with an arrowhead
denoting the direction the object is being passed.
Input and Output Pin
• An object flow must have an object on at least one of its ends.
A shorthand notation for the above diagram would be to use
input and output pins
Data Store
• A data store is shown as an object with the «datastore»
keyword
Expansion Region
• An expansion region is a structured activity region that executes multiple
times. Input and output expansion nodes are drawn as a group of three
boxes representing a multiple selection of items. The keyword iterative,
parallel or stream is shown in the top left corner of the region
Exception Handling
• Exception Handlers can be modeled on activity diagrams as in
the example below
Interruptible Activity Region
• An interruptible activity region surrounds a group of actions that can be
interrupted. In the very simple example below, the Process Order action
will execute until completion, when it will pass control to the Close Order
action, unless a Cancel Request interrupt is received which will pass
control to the Cancel Order action
An Example
• Identify the business logic in the activity diagram shown below
Importance of Activity Diagram
• An activity diagram can depict a model in several ways
• Activity diagram can also be drawn that cross several use cases, or that address just
a small portion of use case
• The activity diagram can be used to develop interaction diagrams which help to
allocate activities to classes
What is a Statechart Diagram?
• A state-chart diagram is used to model the dynamic aspects of the system.
The basic idea is same as the state machine in Finite Automata
• We can draw state diagrams for each object involved in the system
• An object may be in several states of its life time. When a message arrives to
an object (event) it undergoes certain operations (action) or changes its state
• A state diagram shows how an object will react to the arrival of an event
s im p le s t a t e
S ta te 1 S ta te 2
E v e n t1 /A c tio n A
in it ia l s t a t e
E v e n t 2 [g u a r d ] / A c t io n B
E v e n t3
S ta te 3
c o m p o s ite s ta te
f in a l s t a te
Basic Components in a Statechart Diagram
• Two basic elements are there
– Rounded rectangle box representing the state
Nam e
N am e S ta te 1
E n try /A c tio n A E x it /A c tio n C
S ta te V a r ia b le s
E n tr y /A c tio n A
in t e r n a l a c t iv it ie s E v e n t /A c tio n B
E x it /A c tio n C
• If there is no guard or if the guard is true, then for the event the actions will
be followed and it can enter [exit] to [from] a state or remain in the same
state
Statechart Diagram: Example
• Suppose, a COURSE is an object in a usual “Course Registration” use case
(consider the SEIIT). Following is a state diagram to model the behavioral
view of the object COURSE
W ith in
d e a d lin e
P ro p o s e
C o u rs e o ffe r
C o u rs e o p e n
S c h e d u le
fo r
r e g is tr a tio n
D is p a ly
C o u rs e L a s t d a te
a s s ig n e d E n r o ll W ith d r a w
F u ll C lo s e c o u r s e
C o u r s e f u ll
r e g is tr a tio n
S tu d e n t d ro p p e d
V a c c a n c y e x is t
S e s s io n s ta r te d
C o u r s e b e in g
ta u g h t
E v a lu a tio n /S e s s io n e n d
D is p la y Feedback
m a rk s
Advantages of Statechart Diagram
• A statechart diagram is used to model how the state of an
object changes in its life time