1
Web Programming with Java
Java Object-Oriented Programming
Huynh Huu Viet
Email:
Department of Information Systems
2008 © Department of Information Systems - University of Information Technology
2
Outline
Objects and Classes
OOP Concepts
Encapsulation
Aggregation/Composition
Inheritance & Polymorphism
Some useful Java classes
Exception Handling, I/O
Readings & Exercise
Discussion
2008 © Department of Information Systems - University of Information Technology
3
Objects and Classes
Basic OO concepts
Object, class, state, behavior, message
Creating Java classes, objects,
properties and methods
Constructors
Package
Reference data type
Variable scope
2008 © Department of Information Systems - University of Information Technology
4
Basic OO concepts : Object
Object: any thing, which can be described by
State: property, or attribute
Behavior: function/action, or method: ask the object to
perform a task
Example
A student, a desk, a circle, a button, and even a loan
2008 © Department of Information Systems - University of Information Technology
5
Basic OO concepts : Class
Class: a set of objects with common attributes and behaviors
An object is an instance of a class
Class is a template or blueprint that defines what an object's data
and methods will be
2008 © Department of Information Systems - University of Information Technology
6
Basic OO concepts : Message
Objects interact (communicate) by
passing messages
2008 © Department of Information Systems - University of Information Technology
7
Object-Oriented Programming
Class/object is the focus of OOP
At design time
Class is the basic programming unit; a program
consists of one or more classes
Programming focuses on
• Defining classes and their properties
• Manipulating classes/objects properties and behaviors
• Handling objects interactions
At execution time
Programs are executed through predefined
manipulations and interactions
2008 © Department of Information Systems - University of Information Technology
8
Constructor
Constructor is a special method used for
object creation
Circle myCircle = new Circle();
Default constructor
A constructor without any parameter
If a programmer doesn’t define any constructor
for a class, JRE will implicitly create a default
constructor
2008 © Department of Information Systems - University of Information Technology
9
Using Constructor
Defining constructors
Using constructor for default states and
behaviors of an object when it is initially
created
Three differences with methods:
Must have the same name as the class itself.
Do not have a return type - not even void.
Invoked using the new operator when an
object is created.
ClassName() //no “void” or any other data
type
{
…
}
2008 © Department of Information Systems - University of Information Technology
10
Constructor Overloading
Like methods, constructors can be
overloaded
This offers greater flexibility and
convenience of creating objects
2008 © Department of Information Systems - University of Information Technology
11
Using Objects (1)
Object initialization
Using Member Variables
Member variable declaration
• Any where in a class, outside all methods
Used within the class
• Can be used/referenced anywhere directly
Used with objects
• Using the "." operator and preceded with object name
• Ex: myCircle.radius
ClassName objectName;
objectName = new ClassName();
2008 © Department of Information Systems - University of Information Technology
12
Using Objects: using methods
Defining methods
With a return value (type)
Without a return value (type) – void
Calling Methods
Within the class: Called by method name
directly
Used with objects or outside the class
• Using the "." operator and preceded with
object name
• Examples:
–myCircle.getArea()
2008 © Department of Information Systems - University of Information Technology
13
The “main” Method
Following OOP guidelines, the use of the
“main” method should be deemphasized
The “main” method is merely a starting point of the
application
There should not be too many statements in the main
method
Using objects and methods effectively
Typically (and ideally), you only do these
things in the main method
Creating objects
Calling class/object methods
2008 © Department of Information Systems - University of Information Technology
14
Package
Packages are used to group classes
Four reasons for using packages
To locate classes
To avoid naming conflicts
To distribute software conveniently
To protect classes
Packages may be within packages
(hierarchical )
One-to-one mapping of the package
name and the file system directory
structure
2008 © Department of Information Systems - University of Information Technology
15
Java Packages
Java hierarchically organizes classes into
packages
java.lang
java.text
java.util
…
Classes need to be referred using its complete
name (package + class name): for example,
java.util.Calendar
Packages can be “imported” to avoid writing package
names every time you use a class (except java.lang)
import java.util.*;
2008 © Department of Information Systems - University of Information Technology
16
Using Package
Organizing your classes into packages
A class can only be in one package
No duplicate class definition in the same
package
Put the package statement at the beginning
Examples:
package vn.edu.uit.java4web;
package vn.edu.uit.assignment;
package vn.edu.uit.lecture.web;
2008 © Department of Information Systems - University of Information Technology
17
Default Package
A class without any package defined
is in a “default package”
The default package is NOT the root
package!
Classes in the default package cannot be
referenced outside the default package
2008 © Department of Information Systems - University of Information Technology
18
Reference Data Type
Reference data type stores memory
address (reference) as its value
Objects are reference data type
Variable index
object1: 0xf1
object2: 0xf2
object3 …
Data values
0xf1: object1’s content
0xf2: object2’s content
…
2008 © Department of Information Systems - University of Information Technology
19
Object Assignment
Objects are assigned by
reference
Object object1=new Object();
object2=object1;
Variable index
Object object1;
Object object2;
Object object3;
Object content
0xf1: new object1()
0xf2: new object2()
2008 © Department of Information Systems - University of Information Technology
20
Variable Scope
Member variable
Something like a global
variable within the class
Local variable
Method parameter
Method level variable
Block level variable
A variable is effective at
its declaration level and
all sub-levels
2008 © Department of Information Systems - University of Information Technology
21
Variable Overriding
Variables cannot be declared twice at the same
level and its sub-levels
except class member variables (overridden by local
variables)
Use "this" keyword to refer to the class/object
itself to access (the overridden) member variables
this.title
this.couseId
this.getCourseInfo() //”this” can be used with method
It is good to use “this” explicitly for member
variables
2008 © Department of Information Systems - University of Information Technology
22
Outline
Objects and Classes
OOP Concepts
Encapsulation
Aggregation/Composition
Inheritance & Polymorphism
Some useful Java classes
Exception Handling, I/O
Readings & Exercise
Discussion
2008 © Department of Information Systems - University of Information Technology
23
Encapsulation
A concept to hide information or other
details from the outside programs
Why? Should classes and methods
properties be available to other
programs (classes)?
Privacy …
Cohesion, modularity, integrity
Hides complexity, consistent and clear
interaction
2008 © Department of Information Systems - University of Information Technology
24
Access Modifiers
Encapsulation is implemented through the use of access
modifiers/specifiers
private - completely encapsulated within the class
private int length;
default (no modifier) - within the same package
int length;
public - completely open
public int length;
(protected)
2008 © Department of Information Systems - University of Information Technology
25
Access Modifiers for Classes
If a class is defined public, then it can
be referenced anywhere (world
scope)
public class Book { … }
Otherwise, if it is defined “default”,
then it can be referenced only by
those in the same package
class Book { … }
Note: any .java source file can have
only one public class