Object Oriented Programming
(Java)
Module 5
Introduction to Object Oriented
Programming
Learn the behavior and concepts of object and classes.
Understand the concept of Object Orient Design
Learn to use JDeveloper in designing objects relations
Apply different rules in designing Class Diagram given
some relations
Understand the different steps in declaring classes
Learn how to declare classes
Object Oriented Programming
Concepts
5.1
Introduction to Object Oriented Programming
CONCEPT OF OOP
Programming Model:
Ø Procedural Programming
is a technique of solving a problem and
breaking it down into smaller parts and
solving each of the smaller problems.
Introduction to Object Oriented Programming
CONCEPT OF OOP
ØProcedural Programming
This model of software development is
process-centric or procedural since it
concentrates on the procedures in a
system.
Introduction to Object Oriented Programming
CONCEPT OF OOP
ØObject-Oriented Programming
It describes the task to be performed
on objects. These objects that have to
be created and stored in the computer
memory, contain data and instructions to
perform tasks specific to that object.
Introduction to Object Oriented Programming
Procedural vs. OOP
ØThe focus of procedural programming
is to break down a programming task
into a collection of variables, data
structures, and subroutines
ØWhereas in object-oriented
programming, it is to break down a
programming task into objects that
expose behavior (methods) and data
(members or attributes) using
interfaces.
Introduction to Object Oriented Programming
Procedural vs. OOP
ØThe most important distinction is that
while procedural programming uses
procedures to operate on data
structures, object-oriented
programming bundles the two together,
so an "object", which is an instance of a
class, operates on its "own" data
structure.
Introduction to Object Oriented Programming
Procedural vs. OOP
Introduction to Object Oriented Programming
CONCEPT OF OOP
ØObject-Oriented Programming
It uses a collection of objects interacting
with each other.
One of the principal advantages of using
OOP technique is that it enables
programmers to create modules that
need not be changed when a new type
of object is added.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Benefits of OOP:
Ø Reduced software complexities and realistic
modelling through the use of various
techniques:
Abstraction – the process of picking out
the common features of objects and procedures.
This process focuses on the essential
characteristics of an object.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Benefits of OOP:
Ø Reduced software complexities and realistic
modelling through the use of various
techniques:
Encapsulation – the process of hiding the
implementation details of an object.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Benefits of OOP:
Ø Software reusability by implementing
inheritance. Programs are built that can be
used by several other applications.
Inheritance – allows an object to extend its
characteristics to another object.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Benefits of OOP:
Ø Resilience to change with the use of
polymorphism.
Polymorphism – ability to process objects
differently depending on their data type or class.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Class
Object
nA class is a
template or n An object is a
Comparing Classes to Objects running instance of
blueprint that
defines an object’s a class that
attributes and consumes memory
operations and that and has a finite
is created at design lifespan
time
12
123
245
245
Introduction to Object Oriented Programming
CONCEPT OF OOP
Class (Car)
Mitsubishi Honda Mercedes Benz
Objects of Car
Introduction to Object Oriented Programming
CONCEPT OF OOP
Introduction to Object Oriented Programming
CONCEPT OF OOP
Object Relationships:
Ø Association – exists when an object contains a
reference to another object. Referred to as the
“has a” relationship.
Teacher “has a” Student
Introduction to Object Oriented Programming
CONCEPT OF OOP
Object Relationships:
Ø Inheritance – referred to as the “kind of” or “is
a” relationship.
Person
“is a” “is a”
Teacher Student
Introduction to Object Oriented Programming
CONCEPT OF OOP
Object Relationships:
Øsuperclass (base class) – the parent class
from which properties are inherited by another
class
Øsubclass (derived class) – a new class with
properties taken from a parent class
Øabstract class – a well-encapsulated
conceptual class. The objects of this class do
not exist in real world
Introduction to Object Oriented Programming
CONCEPT OF OOP
Unified Modeling Language (UML)
UML is a general-purpose notational
language used for specifying and visualizing
complex software, usually large object-oriented
project. It uses several types of diagrams such
as the case diagram, class diagram, and state
chart diagram.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Class Diagram
A class diagram is used to describe the
attributes and behaviours of objects in the
system.
Introduction to Object Oriented Programming
CONCEPT OF OOP
Attributes and Operations:
Ø Attributes are the data contained in a class
Ø Operations are the actions performed on that data
Ø Accessibility: Public (+), Private (-), Protected (#)
Introduction to Object Oriented Programming
CONCEPT OF OOP
UML Access Modifier Symbols
+ - public
- - private
# - protected
~ - package
““ - no symbol indicates default access
italics - represents abstract class method
underline – represents static method or attribute
Introduction to Object Oriented Programming
CONCEPT OF OOP
Customer
-CustomerID: Long Attributes
+Name: String
+DOB: Date
+AddCustomer( ) Save Operation – No parameters
+GetDetails(ID: Integer) Load Operation – Integer parameter
-CheckValid( ): Boolean
CheckValid Operation – Boolean return value
Introduction to Object Oriented Programming
5.2
Introduction to Object Oriented Programming
Declaring Classes
DECLARING CLASSES
A class definition is consist of several constructs
for defining and declaring the following:
1. Attributes – declared either at the start or end
of a class definition. These variables identify
the data stored in the object.
2. Constructor – a method that is automatically
executed when an object is created. This
method is used to initialize the attributes.
Introduction to Object Oriented Programming
3. Standard Methods – used to change or
access the private data in an object.
• Mutator (Setter) – a method that is used
to change the data.
• Accessor (Getter) – a method that is
used to access or retrieve data.
Introduction to Object Oriented Programming
4. Custom Methods – implement the business
rules for which the application is being
developed.
Class Name
Attributes attributes declaration
Operations or constructor definition
Methods
standard methods definition
custom methods definition
Introduction to Object Oriented Programming
Steps in Declaring Classes:
1. Define the class name
2. Declare the attributes
3. Create constructor
4. Create standard methods
5. Create custom methods
Introduction to Object Oriented Programming
Step 1: Define the ClassName
Syntax:
<access> class <ClassName> {
//codes here
}
Example:
public class Student
{
//codes here…
}
Introduction to Object Oriented Programming
Step 2: Declare the attributes
Syntax:
<access> <datatype> <attributeName>;
Introduction to Object Oriented Programming
Step 3: Create constructor
Syntax:
<access> <ClassName(args list)> {
//code here
}
Introduction to Object Oriented Programming
Step 4: Create standard methods (setters)
Syntax:
<access> <return type> setXxx(args list)
{
//codes here
}
Introduction to Object Oriented Programming
Step 4: Create standard methods (getters)
Syntax:
<access> <return type> getXxx (args list)
{
//codes here
}
Introduction to Object Oriented Programming
Step 5: Create custom methods
Syntax:
<access> <return type> <methodName(args list)> {
//codes here
}
Introduction to Object Oriented Programming
Main class
import java.util.Scanner; System.out.println("Name :"+stud1.getName());
public class Main { System.out.println("Student number: "+
stud1.getStudNumber());
public static void main(String[] args) { System.out.println("Score: "+stud1.getScore());
Scanner input = new Scanner (System.in); stud1.study();
Student stud1 = new Student(); stud1.play(1000);
String message = stud1.doChores()==false?"Do your
System.out.println("Enter your name: "); work!":"Job Well done!";
stud1.setName(input.nextLine()); System.out.println(message);
System.out.println("Enter student number: "); }
stud1.setStudNumber(input.nextInt());
System.out.println("Enter score: "); }
stud1.setScore(input.nextDouble());
Introduction to Object Oriented Programming
The this Keyword
Øthis – contains a reference to the current object being
constructed. It represents an instance of the class in
which it appears. It can be used to access class variables
and methods.
Introduction to Object Oriented Programming
Usage of this keyword:
1. can be used to refer current class instance variable.
2. this() can be used to invoke current class
constructor.
3. can be used to invoke current class method
(implicitly)
Introduction to Object Oriented Programming
Usage of this keyword:
4. can be passed as an argument in the method call.
5. can be passed as argument in the constructor call.
6. can also be used to return the current class instance.
Introduction to Object Oriented Programming
Access Modifiers
Variable: a variable
a) private that is only accessible
b) public within a class where it
is declared.
c) default (no keyword)
Method: a method that
d) protected is only accessible
within the class where
it is declared.
Introduction to Object Oriented Programming
Access Modifiers
Variable: a variable
a) private that is accessible from
b) public all classes.
c) default (no keyword) Method: a method that
is accessible from all
d) protected classes.
Introduction to Object Oriented Programming
Access Modifiers
The default modifier is
a) private accessible only within
b) public the package.
If you don't use any
c) default (no keyword) modifier, it is treated
d) protected as default by default.
Introduction to Object Oriented Programming
Access Specifiers/Modifiers
The protected access
a) private modifier is accessible
b) public within and outside the
package but through
c) default (no keyword) inheritance only.
d) protected It can be applied on
the data member,
method and
constructor. It can't be
applied on the class.
Introduction to Object Oriented Programming
Understanding Java Access Modifiers
Introduction to Object Oriented Programming
Class: a class that can
Modifiers/Qualifiers never be sub-classed
1. final Variable: constant
variable
2. static
Method: a method that
3. abstract cannot be overridden.
4. interface Variable: used in defining
a class variable.
Method: used in defining
a class method.
Introduction to Object Oriented Programming
Classification of Variables:
1. Instance Variables (non-static fields) –
variables stored in each object of a class,
usually referred to as the non-static member
fields of a class.
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
2. Class Variables (static fields) –
variables stored in the class and are
available to all objects of a class or objects
of other classes if access is permitted,
usually referred to as the static members of
the class.
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
3. Local Variables (method variables or
local data) – data used in a method. This
data is temporary and does not exist once
the method has completed execution.
Introduction to Object Oriented Programming
Introduction to Object Oriented Programming
Backiel, A. (2015). Beginning Java Programming: The Object-oriented Approach. 1st Edition
Fain, Y. (2015). Java programming 24-hour trainer. John Wiley and Sons:Indianapolis, IN
Smith, J. (2015). Java programs to accompany Programming logic and design. Cengage Learning:Boston, MA
Yener, M (2015). Professional Java EE Design Patterns. John Wiley & Sons, Inc.
Gordon, S. (2017). Computer Graphics Programming in opengl with Java. Mercury Learning and Information
Butler, A (2017). Java. s.n
Lowe, D (2017). Java all-in-one for dummies. John Wiley and Sons
Saumont, P (2017). Functional Programming in Java: How Functional Techniques Improve Your Java Programs. 1st Edition
Heffelfinger, D. (2017). Java EE 8 Application Development. Packt Puublishing
Murach, J. (2017). Murach’s Java Programming. Mike Murach & Associates,Inc.
Burd, B. (2017). Beginning programming with Java for dummies. John Wiley & Sons, Inc.
Manelli, L. (2017). Developing java web application in a day. s.n.
Klausen, P. (2017). Java 5: Files and Java IO software development (e-book)
Klausen, P. (2017). Java 3 object-oriented programming software development (e-book)
Muller, M (2018). Practical JSF in Java EE 8. Apress