Education and Research
We enable you to leverage knowledge anytime, anywhere!
Object Oriented Programming Using Java -Day 5
Intermediate Level
ER/CORP/CRS/ LA1026 Confidential Ver. No.: 1.1 Copyright © 2008, Infosys Technologies Ltd.
General Guideline
© (2009) Infosys Technologies Ltd.
This document contains valuable confidential and proprietary information of Infosys. Such
confidential and proprietary information includes, amongst others, proprietary intellectual
property which can be legally protected and commercialized. Such information is furnished
herein for training purposes only. Except with the express prior written permission of Infosys,
this document and the information contained herein may not be published, disclosed, or used
for any other purpose.
Copyright © 2008, Infosys Technologies Ltd. 2 Confidential
Confidential Information
This Document is confidential to Infosys Technologies Limited. This document
contains information and data that Infosys considers confidential and proprietary
(“Confidential Information”).
Confidential Information includes, but is not limited to, the following:
Corporate and Infrastructure information about Infosys
Infosys’ project management and quality processes
Project experiences provided included as illustrative case studies
Any disclosure of Confidential Information to, or use of it by a third party, will be
damaging to Infosys.
Ownership of all Infosys Confidential Information, no matter in what media it resides,
remains with Infosys.
Confidential information in this document shall not be disclosed, duplicated or used –
in whole or in part – for any purpose other than reading without specific written
permission of an authorized representative of Infosys.
This document also contains third party confidential and proprietary information.
Such third party information has been included by Infosys after receiving due written
permissions and authorizations from the party/ies. Such third party confidential and
proprietary information shall not be disclosed, duplicated or used – in whole or in part
– for any purpose other than reading without specific written permission of an
authorized representative of Infosys.
Copyright © 2008, Infosys Technologies Ltd. 3 Confidential
Recap of Day 4
Inheritance
Method Overriding
‘final’ and ‘ super’ Keywords
Abstract classes and methods
Copyright © 2008, Infosys Technologies Ltd. 4 Confidential
Session Plan – Day 5
Interfaces
Packages
Object Oriented Analysis and Design
Copyright © 2008, Infosys Technologies Ltd. 5 Confidential
Why Interface ?
The requirement is to define the prototype of all the methods of
the class , leaving the definition/implementation to the user of
the class. How do we implement this?
This is done with the help of an interface
Interfaces are very useful when an unrelated set of classes
have a common set of methods.
Copyright © 2008, Infosys Technologies Ltd. 6 Confidential
Define an interface
Interface can be defined as follows
<<Access specifier>> <<interface>><<interface name>>{
//methods
}
All the methods are public and abstract by default
All the variables are public static final variables by default
Why methods
should be public
and abstract?
Copyright © 2008, Infosys Technologies Ltd. 7 Confidential
Implementing an interface
Interface can be used with the help of ‘implements’ keyword
Once the interface has been defined ,any number of classes can
implement that interface
<<class >><<class name>> <<implements>><<interfacename1>>[,
interfacename2,……]
If the class implements the interface, the class must implement all
the methods defined in an interface or declare itself abstract
Copyright © 2008, Infosys Technologies Ltd. 8 Confidential
Interface Example (1/4)
interface Customer{
void setCustomerName(String name);
String getCustomerName();
}
interface Employee{ Define an Interface
void setEmployeeNo(int no);
int getEmployeeNo();
}
Class Implementing two interfaces
class Trainee implements Customer,Employee{
String bName,cName;
int empNo;
public void setBatchName(String name){
bName=name;
}
Copyright © 2008, Infosys Technologies Ltd. 9 Confidential
Interface Example (2/4)
Implementing an Interface
class Trainee implements Customer,Employee{
String bName,cName;
int empNo;
public void setBatchName(String name){
bName=name;
}
public String getBatchName(){
return bName;
}
Copyright © 2008, Infosys Technologies Ltd. 10 Confidential
Interface Example (3/4)
public void setCustomerName(String name){
cName=name;
}
public String getCustomerName(){
return cName;
} All the methods are overridden in the
public void setEmployeeNo(int no){ implemented class with same
signature
empNo=no;
}
public int getEmployeeNo(){
return empNo;
}
}
Copyright © 2008, Infosys Technologies Ltd. 11 Confidential
Interface Example (4/4)
class InterfaceExample{
public static void main(String args[]){
Trainee trobj=new Trainee();
trobj.setCustomerName("Alphy");
System.out.println("Customer Name:"+trobj.getCustomerName());
trobj.setEmployeeNo(1001);
System.out.println("Employee No:"+trobj.getEmployeeNo());
trobj.setBatchName("Mar09");
System.out.println("Batch Name:"+trobj.getBatchName());
}
}
Customer Name:Alphy
Employee No:1001
Batch Name:Mar09
Copyright © 2008, Infosys Technologies Ltd. 12 Confidential
Interface Example (4/4)
class InterfaceExample{
public static void main(String args[]){
Trainee trobj=new Trainee();
trobj.setCustomerName("Alphy");
System.out.println("Customer Name:"+trobj.getCustomerName());
trobj.setEmployeeNo(1001);
System.out.println("Employee No:"+trobj.getEmployeeNo());
trobj.setBatchName("Mar09");
System.out.println("Batch Name:"+trobj.getBatchName());
}
}
Customer Name:Alphy
Employee No:1001
Batch Name:Mar09
Copyright © 2008, Infosys Technologies Ltd. 13 Confidential
‘extends’ and ‘implements’ with class
A class can extend from another class and at the same time can
implement any number of interfaces
<<class>> <<class name>>extends <<class name>><<implements>><<interface name1>>[,
interface name2>>,……]{
//methods
}
Copyright © 2008, Infosys Technologies Ltd. 14 Confidential
interface Inheritance
An Interface can extend from one another interface with the help of
the keyword extends
<<interface>> <<interface name>> <<extends>><<interface name>>{
//methods
}
Copyright © 2008, Infosys Technologies Ltd. 15 Confidential
Abstract Classes vs Interfaces
Abstract Classes Interfaces
Can have concrete methods Can have only abstract methods
Can have variables Can have only static final (constant) data
members
Can have private and protected All members are public abstract by
members default
Can be extended from one class Can be extended from any number of
interfaces
A class can extend only one abstract A class can implement any number of
class interfaces
Copyright © 2008, Infosys Technologies Ltd. 16 Confidential
Can you Answer ?
Say true or false
An abstract class can have a non abstract method
If the class has one abstract method then that class should be
declared abstract
All the methods of an interface are abstract by default
All the methods of an abstract class is abstract by default
Interfaces can extend from any number of interfaces
A local variable can be defined in an interface
A class can implement any number of interfaces
An object of an interface can be created
Copyright © 2008, Infosys Technologies Ltd. 17 Confidential
Packages
In the Retail Application, let us assume that the code has
been written by programmers who are in different teams
which are distributed. When the code is integrated, they
expect name clashes to occur between the class names
used . Further security of data is required between classes
written by different programmers. We need to -
Identify a means segregating the classes logically and implement
access specfiers for security
We will learn–
Packages
Copyright © 2008, Infosys Technologies Ltd. Confidential
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Packages
ER/CORP/CRS/ LA1026 Confidential Ver. No.: 1.1 Copyright © 2008, Infosys Technologies Ltd.
Packages
Packages are Java’s way of grouping a number of related classes
and interfaces together into a single unit
Different type of Packages
Built in package
User defined package
Copyright © 2008, Infosys Technologies Ltd. 20 Confidential
Java Program Structure
package
classes
class/instance variables
methods
local variables
statements
…….
Copyright © 2008, Infosys Technologies Ltd. 21 Confidential
Accessing Classes from the Packages
There are two ways to access a class associated with package
Method 1:
Using fully qualified class name
– java.lang.lang.Math.sqrt(varOne);
Method 2:
Import package and use class name directly
– import java.lang.Math
– Math.sqrt(varOne);
Copyright © 2008, Infosys Technologies Ltd. 22 Confidential
User Defined Packages
Package can be created with the help of ‘package’ keyword
package <<package name>>;
public class classname{
//Code goes here
}
class classname{
}
Package should be the first statement in the source file(.java)
Copyright © 2008, Infosys Technologies Ltd. 23 Confidential
User Defined Packages
Sub package can be created as follows:
package <<first package name>>.<<second package name>>;
public class classname{
//Code goes here
}
class classname{
}
Copyright © 2008, Infosys Technologies Ltd. 24 Confidential
Adding a Class to a Package
A package can be spread across different .java files
In one .java file, there could be only one class declared public and
that becomes the name of the .java file
A package can however have several public classes spread across
multiple .java files.
More than one class/interface can have the same name but it
should be in a different package
Accessing user defined package can be done by using any of the
following methods:
Using fully qualified class name
Import package and use class name directly
Copyright © 2008, Infosys Technologies Ltd. 25 Confidential
Accessing a User Defined Package (1/3)
Using fully qualified class name
Package statement
package addition;
public class AddTwo{
private int sum;
public void calSum(int varOne,int varTwo){
sum=varOne+varTwo;
System.out.println("Sum= "+sum);
}
}
Copyright © 2008, Infosys Technologies Ltd. 26 Confidential
Accessing a User Defined Package (2/3)
Package statement
package calculation;
class PackEx{
fully qualified class name
public static void main(String args[]){
addition.AddTwo obj=new addition.AddTwo();
obj.calSum(23,46);
}
}
Sum=69
Copyright © 2008, Infosys Technologies Ltd. 27 Confidential
Accessing a User Defined Package (3/3)
Import package and use class name directly
Package statement (first statement)
package addition;
import statement
import addition.AddTwo;
class PackEx{
public static void main(String args[]){
AddTwo obj=new AddTwo();
obj.calSum(23,46);
}
Sum=69
}
Copyright © 2008, Infosys Technologies Ltd. 28 Confidential
Access Modifiers - Revisited
Java has 4 access control modifiers
private: Accessible only within the class
default: No keyword, Accessible only within the package
protected: Similar to default with the addition that available to all
child classes; that is, even if child class is in a different package
public: Accessible to all
Data Members and Methods can have any of these specifiers
Classes and Interfaces can have either the public access or the
default access
Copyright © 2008, Infosys Technologies Ltd. 29 Confidential
Access Control
Accessible to public protected default private
Same class Yes Yes Yes Yes
All classes in the same package Yes Yes Yes No
All sub classes in the different package Yes Yes No No
All classes in the different package Yes No No No
Copyright © 2008, Infosys Technologies Ltd. 30 Confidential
Built in package(1/3)
These packages provides the set classes , interface and method
for the programmer to develop an application in an easier way
Programmer can reuse everything from the package and save
effort
Few examples of built in packages
java.lang
java.io
java.sql
java.awt
java.net
Copyright © 2008, Infosys Technologies Ltd. 31 Confidential
Built in package(2/3)
java.lang
Contains classes that are essential for developing basic Java
programs
The String class, System class etc, belong to this package
There is no need to explicitly import this package
java.io
Helps to perform various input and output operations
Java.sql
Provides classes and interface that helps to connect to the
database like SQL server, oracle easily
Copyright © 2008, Infosys Technologies Ltd. 32 Confidential
Built in package(3/3)
java.awt
Classes for implementing GUI Application eg: windows,
buttons, menus etc.
java.net
Classes for networking
Copyright © 2008, Infosys Technologies Ltd. 33 Confidential
Uses of Packages
Logical grouping of classes and interfaces
How to avoid
name
Avoiding name clashes clashes?
Provides an extra level of protection to its members
How can
extra level of
security be
provided
Copyright © 2008, Infosys Technologies Ltd. 34 Confidential
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Object Oriented Analysis and Design &UML
ER/CORP/CRS/ LA1026 Confidential Ver. No.: 1.1 Copyright © 2008, Infosys Technologies Ltd.
Terminology…
Object- This is an analysis method in which the
Oriented requirements are mapped to the perspective of
Analysis classes and object suiting the domain of the
(OOA) requirements
Object- This represents the process of object-oriented
Oriented approach which also provides a notation for
Design depicting the system under design
(OOD)
Object- This represents the implementation of OO
Oriented concepts in terms of a co-operative collection
Programming of objects . These in turn are instances of
(OOP) classes . The classes in turn may form a
hierarchy
36
Copyright © 2008, Infosys Technologies Ltd. 36 Confidential
Object Oriented Process(Developer’s Perspective )
Identify the
classes and
Objects
Implement Identify the
the classes behavior and
and objects attributes
Identify the
relationships
This figure shows the object oriented development process which is
followed by developers
37
Copyright © 2008, Infosys Technologies Ltd. 37 Confidential
What is a Model?
A model is an abstract representation of a system
constructed from a perspective to understand the system prior
to building or modifying it.
A static (or structural) model can be seen as a “picture" of a
system's parameters at a specific point in time.
A dynamic (or behavioural) model is a collection of
procedures or behaviours that, taken together, reflect the
behaviour of a system over time
Copyright © 2008, Infosys Technologies Ltd. 38 Confidential
Advantages of Modelling
Models make it easier to express complex ideas
For example, an architect builds a model to communicate
ideas more easily to clients
The cost of the modelling analysis is much lower compared
to the the cost of similar experiment conducted with a real
system
Models enhance learning and better understanding of the
real system
Model has scope for modification
Copyright © 2008, Infosys Technologies Ltd. 39 Confidential
The Unified Modelling Language (UML)
“The Unified Modelling Language (UML) is a language for specifying,
constructing, visualizing, and documenting the software system and its
components”[OMG03a]
Visual
Document
UML is a graphical
language Enables documentation of
system architecture and
Notations of UML are well- details
defined
UML
Construct Specify
Enables mapping from a Building models that are
model in the UML to OO precise, unambiguous
Languages such as Java,
C++, or C#
Copyright © 2008, Infosys Technologies Ltd. 40 Confidential
UML Diagrams
UML diagrams are the notations used to represent the model in
different stages of the OO development process
In UML, there are nine standard diagrams used in different
phases
Use Case Diagram
Class Diagram
Object Diagram
State Diagram
Component Diagram
Deployment Diagram
Collaboration diagram
Sequence Diagram
Activity diagram
Copyright © 2008, Infosys Technologies Ltd. 41 Confidential
Use Case Diagram
Use-case: a technique to capture business process from the
user’s perspective i.e., a way of documenting system
functionality expected by the user
Jacobson: “A use-case is a processing sequence which the system
is required to perform to fulfil a functionality desired by a user”
Helps in identifying classes
Copyright © 2008, Infosys Technologies Ltd. 42 Confidential
Use Case Diagrams
Term Meaning Notation used Example
System Something that performs a Retail System
function
Actors Represent the users of the Customer
system
Use Cases Represent the actions
/activities that a user takes
on the system ie. Pay Bill
functionality
Copyright © 2008, Infosys Technologies Ltd. 43 Confidential
Use Case Diagrams
Term Meaning Notation used Example
Relationships Connects the users to the use
cases Pays Bill
Customer
Generalization
(included in a hierarchy) Customer
Specialization(included in a Registered Regular
Customer Customer
hierarchy)
include
Pays Bill Purchase
Customer
Copyright © 2008, Infosys Technologies Ltd. 44 Confidential
Use Case Diagram- A complete example
Steps to create a Use Case diagram
•Identify the actors in the system
•Identify the use cases
•Identify the relationships
•Create a Use Case diagram
Copyright © 2008, Infosys Technologies Ltd. 45 Confidential
Class Diagram
Collection of classes along with the collaborations and
relationships among classes
Class Diagrams has three compartments –
Classifier Name
Attributes
Operations
Copyright © 2008, Infosys Technologies Ltd. 46 Confidential
Class Diagram – Components
Customer
Name of the class
- Represents private
-customerNo:int
access specifier
-customerName:String
+getCustomerNo():int Attributes of the class
+setCustomerNo(int)
+ Represents public +getCustomerName():String
access specifier Methods or activities in
+setCustomerName(String) the class
+displayBillAmount()
Represents is-a
relationship
RegisteredCustomer RegularCustomer
-dateOfRegistration:date -creditPoint:int
-discount:float +getCreditPoint():int
+getDateOfRegistration():Date + setCreditPoint(int)
+ setDateOfRegistration(Date)
+getDiscount():float
+setDiscount(float)
Copyright © 2008, Infosys Technologies Ltd. 47 Confidential
Class Diagrams
Term Meaning Notation used
Class The name of the class, Class Name
Attributes
attributes and methods
Methods
Relationships is-a relationship
Generalization
Specialization
has-a
relationship(Aggregation)
Uses –a
relationship(Association)
Copyright © 2008, Infosys Technologies Ltd. 48 Confidential
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Object Oriented Tools
ER/CORP/CRS/ LA1026 Confidential Ver. No.: 1.1 Copyright © 2008, Infosys Technologies Ltd.
UML Tools
IBM Rational Rose
Rational Rose modeling tool is from IBM Rational Software
Corporation. Rose stands for "Rational Object-oriented Software
Engineering") is a visual modeling tool for UML
Together Control Center:
Together Control Center (formerly from Togethersoft) from Borland is
an entire suite of visual modeling tools for UML
Poseidon:
Poseidon from Gentleware has its roots in the ArgoUML open source
project which evolved as an open source effort and is a useful, full-
featured UML tool freely available under the Open Publication
License
Copyright © 2008, Infosys Technologies Ltd. 50 Confidential
Learning approach
The following are strongly suggested for a better learning and
understanding of this course:
Noting down the key concepts in the class, explained by the
educator
Analyze all the examples / code snippets provided
Study and understand the self study topics
Completion and submission of all the assignments, on time
Completion of the self review questions in the lab guide
Study and understand all the artifacts including the reference
materials / e-learning / supplementary materials specified
Completion of the project (if applicable for this course) on time
inclusive of individual and group activities
Taking part in the self assessment activities
Participation in the doubt clearing sessions
Copyright © 2008, Infosys Technologies Ltd. 51 Confidential
Summary
Interfaces
Packages
Object Oriented Analysis and Design
Copyright © 2008, Infosys Technologies Ltd. 52 Confidential
Thank You
“The contents of this document are proprietary and confidential to Infosys Technologies Ltd. and may
not be disclosed in whole or in part at any time, to any third party without the prior written consent of
Infosys Technologies Ltd.”
“© 2008 Infosys Technologies Ltd. All rights reserved. Copyright in the whole and any part of this
document belongs to Infosys Technologies Ltd. This work may not be used, sold, transferred, adapted,
abridged, copied or reproduced in whole or in part, in any manner or form, or in any media, without the
prior written consent of Infosys Technologies Ltd.”
Copyright © 2008, Infosys Technologies Ltd. 53 Confidential
Education and Research
We enable you to leverage knowledge anytime, anywhere!
Appendix
ER/CORP/CRS/ LA1026 Confidential Ver. No.: 1.1 Copyright © 2008, Infosys Technologies Ltd.
Appendix - UML Diagrams
UML Diagrams Meaning of the diagrams
Use Case Diagram Shows use cases, actors, and their interrelationships
Class Diagram Shows a collection of classes and types, their contents, and their
relationships
Object Diagram Depicts objects and their relationships at a point in time, it is a special
case of a class diagram
State Diagram Describes the states an object and the transitions between states
Component Diagram Depicts the components that compose an application, their
interrelationships, interactions, and their public interfaces are shown
in this diagram.
Deployment Diagram Shows how the application is deployed, the hardware or software
execution environments, as well as the middleware connecting them
Collaboration diagram Shows how the objects send and receive messages, ie.
Communication
Sequence Diagram Shows sequence/time ordering between messages
Activity Diagram Shows high-level business processes, data flow, and how to model
the logic within the system
Copyright © 2008, Infosys Technologies Ltd. 55 Confidential