Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

OBJECT ORIENTED

PROGRAMMING
TOPICS
1. INTRODUCTION 2. FUNDAMENTALS
TO OBJECT- OF THE JAVA 3. CLASSES AND
ORIENTED PROGRAMMING OBJECTS
PROGRAMMING LANGUAGE

5. MULTI-
4. INHERITANCE
THREADING AND
AND
EXCEPTION
POLYMORPHISM
HANDLING
TOPIC 1
INTRODUCTION TO
OBJECT-ORIENTED
PROGRAMMING
Sub Topics

a. Programming techniques.
b. Object Oriented concepts and
terminologies.
c. Notation that is used for OOAD by
Unified Modelling Language (UML).
d. Class diagram.
Topic 1
Understand the Programming
Programming Techniques
Techniques
History of Java technology
Unstructured Programming
Understand Java Features of Java programming language
• terminology
Small andandsimple programs
Anatomy ofconsisting only of one main
the Java programs
program
environment Programming style and documentation
Write, compile
• A sequence of commands and run javawhich
or statements programmodify
Errors in java
data (global) throughout the whole program

Procedural Programming
• A subtype of imperative programming as a programming
paradigm based upon the concept of procedure calls, in
which statements are structured into procedures (also
known as subroutines or functions).
• Break down a programming task into a collection of
variables, data structures, and subroutines
Topic 1
Understand the Programming
Programming Techniquest
Techniques
History of Java technology
Object Oriented Programming (OOP)?
Understand Java Features of Java programming language
terminology and Anatomy of the Java programs
• A type of
environment
programming in which programmers define not only
Programming style and documentation
the data type of Write,
a data structure,
compile and runbut
java also the types of
program
operations (functions) that
Errors in can
javabe applied to the data structure.
• In this way, the data structure becomes an object that includes
both data and functions.
Topic 1
Understand the Programming
Programming Techniquest
Techniques
History of
Unstructured Programming Java
Language technology
Structured Programming Language
Understand Java Features of Java programming language
terminology
Also known as and
Non-structured Anatomy
Programmingof the Java programs
Modular Programming
environment Programming style and documentation
None. It is the earliest programming
Subset of Write, compile and runProcedural
java program
Programming
paradigm.
Errors in java
To enforce a logical structure on the
program being written to make it more
Purpose Just to code.
efficient and easier to understand and
modify.

The entire program must be coded in one Divides the program into smaller units or
Programming
continuous block. modules.
Structured programming, specifically
Precursor to procedural programming and then object- Object-oriented programming (OOP)
oriented programming.
Producing hardly-readable (“spaghetti”)
Code Produces readable code
code

Sometimes considered a bad approach for Usually considered a good approach for
For Projects
creating major projects creating major projects
Topic 1
Understand the Programming
Programming Techniquest
Techniques
History of Java technology
Understand Java Features of Java programming
Unstructured Programming Language
language
Structured Programming Language
terminology and Anatomy of the Java programs
environment Programming
Offers freedom to programmers to program as theystyle and documentation
Freedom Has some limitations
Write, compile and run java program
want

Allowed data Errors


Non-structured languages allow in
onlyjava
basic data
Structured languages allow a variety of data
types, such as numbers, strings and arrays
types types.
(numbered sets of variables of the same type).

Modify and
Very difficult to modify and to debug Easy to modify and to debug
debug

early versions of BASIC (such as MSX BASIC and GW-


BASIC), JOSS, FOCAL, MUMPS, TELCOMP, COBOL,
machine-level code, early assembler systems C, C+, C++, C#, Java, PERL, Ruby, PHP, ALGOL,
Languages
(without procedural metaoperators), assembler Pascal, PL/I and Ada
debuggers and some scripting languages such as
MS-DOS batch file language.
Topic 1
Understand the Programming
Object Oriented Approach
Techniques
History of Java technology
The key ideas
Understand Java of the object oriented approach are :
Features of Java programming language
terminology and Anatomy of the Java programs
• environment
Objects Programming style and documentation
Write, compile and run java program
• Encapsulation Errors in java

• Class and Inheritance


• Instances and Instantiation
• Methods and Messages
Topic 1
Understand the Programming
Benefits of OOP Approach
Techniques
History of Java technology
• Code extensibility
Understand Java Features of Java programming language
terminology
• It is and Anatomy
also extensible, of the
as objects canJava programs
be extended to include new
environment Programming style and documentation
attributes and behaviors.
Write, compile and run java program
• Code reusability Errors in java
• Objects created for Object Oriented Programs can easily be reused in
other programs.
• Represent real world
• In Object Oriented Programming we are trying to model either real
world entities or processes and represent them in software.
• Data Security
• This characteristic of encapsulation (data hiding) provides greater
system security and avoids unintended data corruption
Topic 1
Understand the Programming
Object Oriented Analysis & Design
Techniques
History of Java technology
Understand Java Features of Java programming language
terminology and Anatomy of the Java programs
environment Programming style and documentation
Write, compile and run java program
Errors in java
Topic 1
Understand the Object Oriented
Object Oriented Languages
Concepts & Terminologies
History of Java technology
• Understand
SimulaJava- the first object-oriented programming
Features of Java programming language
terminology and Anatomy of the Java programs
language.
environment Programming style and documentation
Write, compile and run java program
• Java, Python, C++,Visual Basic .NET, Ruby - the
Errors in java
most popular OOP languages today.
• The Java programming language is designed
especially for use in distributed applications on
corporate networks and the Internet.
• Ruby - used in many Web applications.
Topic 1
Understand the Object Oriented
Object, attribute and behavior
Concepts & Terminologies
History of Java technology
• What is an object?
Understand Java Features of Java programming language
terminology of a class.Anatomy of the Java programs
– Instanceand
environment Programming style and documentation
– Can be defined asWrite,
a thing.
compile and run java program
– Set of attribute andErrors in java
behavior.

• What is an attribute?
– Characteristic of the object
– State of an object

• What is behavior?
– Process or operation of the object that usually modify the state
of an attribute.
Topic 1
Understand the Object Oriented
Object, attribute and behavior
Concepts & Terminologies
History of Java technology
•Understand
Examples Java of object, attributes and behavior.
Features of Java programming language
terminology and Anatomy of the Java programs
environment Programming style and documentation
Write, compile and run java program
Errors in java

Object Car
Attribute -Color
-No of tire
-No of door
-Chassis
Operation -Drive, stop, reverse
Topic 1
Understand the Object Oriented
Basic terminologies of OOP
Concepts & Terminologies
History of Java technology
Classes
Understand Java Features of Java programming language
• terminology
A class is a and Anatomy
blueprint or prototypeof the Javawhich
from programs
objects are created.
environment Programming style and documentation
Objects with similar properties and methods are grouped together
Write, compile and run java program
to form a Class Errors in java
• A set of objects that have similar attributes and methods.
• Attributes and methods of a class can be used by each object from
that class.
class Student class Box
{ String name, address, status; { double width, height, depth; data
data
int icno, sid;
double marks; double ComputeVolume()
method { return( width * height * depth ); }
char DetermineStatus() method
{ if marks >= 40 double ComputeArea()
status = “Pass”; { return( width * height ); }
else }
status = “Fail”; Class
}
} Example 2 : Definition of Class Box
Example 1 : Definition of Class Student
Topic 1
Understand the Object Oriented
Basic terminologies of OOP
Concepts & Terminologies
History of Java technology
Object
Understand Java Features of Java programming language
• terminology Anatomy
and has state
Any entity that of the Java programs
and behavior.
environment Programming style and documentation
• Object is the term used to explain many things. Example: student,
Write, compile and run java program
chair and circle. Errors in java
• An Object consists of data and method
• Properties of an object are called data. In the real world,
characteristics of an object can be divided into two types:
▪ Data that can be seen such as a human with two hands.
▪ Data that cannot be seen such as a human with a name.
• Method is a set of function that manipulates data, such as
method DetermineStatus() can determine exam result for object
student
Topic 1
Understand the Object Oriented
Basic terminologies of OOP
Concepts & Terminologies
History of Java technology
Encapsulation
Understand Java Features of Java programming language
terminology and
• Encapsulation Anatomy
is a process of of the all
tying together Java
dataprograms
and methods that form a
environment
class and control the accessProgramming style
to data by hiding itsand documentation
information.
Write, compile and run java program
• The wrapping up of data and its functions into a single unit is called
Errors in java
Encapsulation.
• It enables access to object just by using methods of that object.
• It is one of the security features in OOP.
• If the college management wants to
Class Student get the status whether a student “pass”
or “fail”, they only have to know the
Name, Student ID, Address, IC No
status without knowing how to
Calculate_result() determine or calculate the grade.
• So, this is a way of implementing
Determine_grade() encapsulation where the code in the
program is hidden thus to prevent from
Print_result() being modified.
Topic 1
Understand the Object Oriented
Basic terminologies of OOP
Concepts & Terminologies
History of Java technology
Inheritance
Understand Java Features of Java programming language
• terminology Anatomy
andclass from
Create a new of the class
an existing Java programs
together with new
environment Programming style and documentation
attributes and behaviours.
Write, compile and run java program
• Inheritance is the process
Errors of forming a new class from an existing
in java
class or base class.
• The new class will have the same ability as the base class.
• Use the concept of code reusability.
Class A Base class to class B

Derived class to class A


Class B
Base class to C, D and E

Class C Class D Class E Derived class to class B


Topic 1
Understand the Object Oriented
Basic terminologies of OOP
Concepts & Terminologies
History of Java technology
Data Abstraction
Understand Java Features of Java programming language
terminology
• is a process of hiding Anatomy of
and the implementation the Java
details programs
from the user, only the functionality
environment
will be provided to the user. Programming style and documentation
• In other words user will haveWrite, compileon
the information and run
what thejava program
object does instead of how it
does it. Errors in java
• Data Abstraction also represents the needed information in the program without
presenting the details.
Topic 1
Understand the Object Oriented
Basic terminologies of OOP
Concepts & Terminologies
History of Java technology
Polymorphism
Understand Java Features of Java programming language
terminology and
• Polymorphism is a processAnatomy of the
of giving the sameJava programs
message to two or more
environment
different Programming
objects and produce style and depending
different behaviours documentation
on how the
Write, compile and run java program
objects receive the message.
Errors in java
• Polymorphism allows routines to use variables of different types at different
times.
• The word "polymorphism" means "many forms“ - the ability to appear in many
forms.

Example 1:
Message: Withdraw a money from bank

Action Object
Student1 : Using ATM machine from Bank account.

Student2 : Using ATM Machine from other Bank (MEPS)


Topic 1
Understand the Object Oriented
Abstraction vs encapsulation
Concepts & Terminologies
History of Java technology
Understand
AbstractionJava Features of Java programming language
Encapsulation
terminology and Anatomy of the Java programs
Delete all unnecessary
environment
Control the access to data by
Programming style and documentation
data and remain the hiding its information
Write, compile and run java program
necessary data only Errors in java
is used to hide certain is a process of binding or
details and only show the wrapping the data and the codes
essential features of the that operates on the data into a
object. single entity.
You do abstraction when You do encapsulation when
deciding what to hiding something that you have
implement. implemented
Topic 1
Understand the Object Oriented Structured Programming vs OOP
Concepts & Terminologies approach
History of Java technology
Understand Java Features of Java programming Object Oriented
language
Structured Programming
terminology and Anatomy of the Java programs
Programming
environment Programming style and documentation
Write, compile
Based on programmer definition andObject
function run java program
Oriented Programming
Errors infunctions.
Split a big program to several java Technique
1 Each function will perform the task more The use of encapsulation
specific. concept combined data and
function in one component (
class).

Program code cannot be reused Allowed code reusability


Each function assign to do specific task It can be done through
2 only. We must accept the function as it inheritance technique. This
is. To modify it, the code need to copy technique allowed object to
and modify it based on it requirement inherits characteristic (function
and data) other object.
Topic 1
Understand the Object Oriented Structured Programming vs OOP
Concepts & Terminologies approach
History of Java technology
Structured
Understand JavaProgramming
Features of Java Object Oriented
programming Programming
language
terminology and
Data manipulation Anatomy of Using
function the Java programs to react on data
encapsulation
environment Programming
Data will be send to specific style and documentation
Encapsulation is used to package data
3 function to be manipulate where
Write, compile with
andfunction that
run java will take an action
program
the function is not determine first.
Errors in java over the data. It define which function
will be perform to each object. Class
will control any operation done to data
and function inside it.
No data control access. Restriction in retrieving data.
Main function will access all data Access control exist for each data in
4 and function in the program. the class.

Not supported polymorphism Used polymorphism


Each data must be declared Allowed one function to be executed
5 before do any operation on it. with various method.
Topic 1
Understand the Object Oriented Object-oriented Analysis and Design
Concepts & Terminologies (OOAD)
History of Java technology
• OO Analysis
Understand Java Features of Java programming language
terminology and Anatomy of the Java programs
– To find and describe
environment the objects or concepts in the problem domain.
Programming style and documentation
– For example in theWrite,
Librarycompile and run
Information java program
System, some of the
Errors Library
concepts include Book, in java and User.

• OO Design
– To define software objects (attributes and operations) and how they
collaborate to fulfill the requirements that will be implemented in
an OO programming language.
– For example in the Library Information System, a Book is an object
that have a title as it attribute and getChapter as the method.
Topic 1
Understand the Object Oriented Object-oriented Analysis and Design
Concepts & Terminologies (OOAD)
History of Java technology
• OOAD
Understand Java Features of Java programming language
terminology and
– Object-oriented Anatomy
analysis andofdesign
the Java programs
(OOAD) is a software
environment Programming stylea and documentation
engineering approach that models system as a group of
Write, compile and run java program
interacting objects.
Errors in java
– Each object represents some entity of interest in the system being
modeled, and is characterized by its class, its state (attribute), and
its behavior.
– Various models can be created to show the static structure,
dynamic behavior, and run-time deployment of these collaborating
objects. There are a number of different notations for representing
these models, such as the Unified Modeling Language (UML).
• OO Programming
– Design components are implemented.
Topic 1
Understand the Object Oriented
Unified Modelling Language (UML)
Concepts & Terminologies
History of Java technology
• What is UML?
Understand Java
terminology and
Features of Java programming language
Anatomy of the Java programs
• The Unified Modeling
environment Programming style andor
Language documentation
UML is a language
Write, compile and run java program
for specifying, Errors
visualizing,
in java constructing and
documenting the artifacts of software systems, as
well as for business modeling and other non-software
system.
• It is also a graphical modeling language that is used to
express designs.
Topic 1
Understand the Object Oriented
Unified Modelling Language (UML)
Concepts & Terminologies

Diagram History of Java technology


Description
Understand Java Features of Java programming language
Use case diagram show actors (people or other users of the system), use cases
terminology and Anatomy
(the of when
scenarios the Java
they programs
use the system), and their
environment Programming style and documentation
relationships
Class diagram Write,
The compile
class diagramand runtojava
is core program design. It
object-oriented
Errors in java
describes the types of objects in the system, show classes
and the relationships between them
Sequence diagram show objects and a sequence of method calls they make
to other objects.
Collaboration diagram show objects and their relationship, putting emphasis on
the objects that participate in the message exchange

State diagram show states, state changes and events in an object or a


part of the system
Activity diagram show activities and the changes from one activity to
another with the events occurring in some part of the
system
Entity relationship diagram show data and the relationships and constraints between
the data.
Topic 1
Understand the Object Oriented
UML elements
Concepts & Terminologies
History of Java technology
• What are UML elements?
Understand Java Features of Java programming language
terminology and Anatomy of the Java programs
– UML offers
environment
a standard way to visualize a system's architectural
Programming style and documentation
blueprints, including elements
Write, compile such as:java program
and run
– activities Errors in java
– actors
– business processes
– database schemas
– logical components
– programming language statements
– reusable software components.
Topic 1
Understand the Object Oriented
UML Class Diagram
Concepts & Terminologies
History of Java technology
• A class is represented by a rectangle having three
Understand Java Features of Java programming language
terminology and Anatomy of the Java programs
sections :
environment Programming style and documentation
Write,the
– the top section containing compile and
name of therun java program
class
Errors in java
– the middle section containing class attributes
– the bottom section representing operations of the class
• Syntax: Example:
ClassName Shirt

shirtID
Attributes Price
Description
Size
Operations colorCode R=Red, B=Blue, G=Green
calculateShirtID()
displayInformation()
Topic 1
Understand the Object Oriented
UML Class Diagram
Concepts & Terminologies
History of Java technology
• Include data type for data/ method
Features of Java programming language
Understand Java
terminology and Anatomy of the Java programs
•environment
Include notation for data Programming
access style and documentation
• + for public data/Write,
method compile and run java program
• - for private data/Errors
methodin java

Shirt
- shirtID : int
- Price : float
- Description : string
- Size : string
+ calculateShirtID() : int
+ displayInformation() : void
Topic 1
Understand the Object Oriented
Components of a class
Concepts & Terminologies
History of Java technology
•Understand
Structure Java
of a class
Features of Java programming language
terminology and Anatomy of the Java programs
environment
class Dog{ Programming styleClass declaration
and documentation
private int age; Write, compile and run
Variables java program
declaration
private String color; Errors in java

public void eat()


Methods
{
}
Dog
public void sleep()
{ - Age : int
} - Color : String Class diagram
}
+ eat() : void
Class + sleep() : void
Topic 1
Understand the Object Oriented
Exercise 1
Concepts & Terminologies
History of Java technology
•Understand
Draw aJavaclass diagram for a class called Circle
Features of Java programming language
with these
terminology Anatomy of the Java programs
and information:
environment Programming style and documentation
❖Two private instance variables:
Write, compile and runradius (of the
java program
Errors in java
type double) and color (of the type String), with
default value of 1.0 and "red", respectively.
❖Two public methods: getRadius() and getArea(),
which return the radius and area of this instance,
respectively.
Topic 1
Understand the Object Oriented
Exercise 2
Concepts & Terminologies
History
• Draw a class diagram based onofthe
Java technology
coding below:
Understand
publicJavaclass Features
Car { of Java programming language
terminology and Anatomy of the Java programs
private String
environment carColor;
Programming style and documentation
private double carPrice
Write, compile and run=java
0.0;
program
public StringErrors ingetCarColor
java (String model)
{
return carColor;
}

public double getCarPrice (String model)


{
return carPrice;
}
}
Topic 1
Understand the Object Oriented
Exercise 3
Concepts & Terminologies
History of Java technology
Write javaJava
Understand code based on the UML class diagram
Features of Java programming language
terminology and Anatomy of the Java programs
below:
environment Programming style and documentation
Write, compile and run java program
Errors in java

Student
- Name : String
+ displayName() : void

You might also like