Chapter 4 - Part 1
Chapter 4 - Part 1
Learning Outcomes
At the end of this lesson, you should be able to
1. Introduction to OO Programming
Concepts
Object-oriented programming (OOP) involves
programming using objects.
Classes
Classes are constructs that define objects of the same
type.
A class uses
variables to define data fields and
methods to define behaviors.
An object is an instance of a class
Classes
A class can also provide a special type of methods, known as
constructors, which are invoked to construct objects from the
class.
class Circle {
/** The radius of this circle */
double radius = 1.0;
/** Construct a circle object */
Circle() {
}
Data field
Constructors
Method
}
6
UMLClass Diagram
Class name
radius: double
Data fields
Circle()
Constructors and
Methods
Circle(newRadius: double)
getArea(): double
circle1: Circle
radius: 10
circle2: Circle
radius: 25
circle3: Circle
UMLnotation
for objects
radius: 125
7
myCircle
null
Example:
Circle myCircle;
Note : no circle object has been created.
8
Creating Object
To actually create a Circle object, use the following
statement.
reference value
: Circle
radius: 5.0
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();
Example:
Assign object reference
Create an object
reference value
: Circle
radius: 5.0
10
Accessing Objects
myCircle
reference value
: Circle
radius: 5.0
e.g.,
Circle myCircle = new Circle(10);
System.out.println(myCircle.radius);
3. Constructors
Constructors must have the same name as the class itself.
Constructors do not have a return type - not even void.
class Circle {
/** The radius of this circle */
double radius
Circle()
{ = 1.0;
Circle(double
newRadius)
/** Construct a circle
object */ {
Circle(double
newRadius) {
radius
=
newRadius;
radius = newRadius;
} }
/** Return the area of this circle */
double getArea() {
return radius * radius * 3.14159;
}
Data field
Constructors
Method
}
12
Constructors
A constructor with no parameters is referred to as a noarg constructor.
class Circle {
/** The radius of this circle */
double radius = 1.0;
/** Construct a circle object */
Circle() {
}
Data field
Constructors
Method
13
a cir cle
circl e */
objec t */
new ClassName();
Example:
new Circle();
new Circle(5.0);
14
Default Constructor
A class may be declared
without constructors. In
this case, a no-arg
constructor with an
empty body is implicitly
declared in the class.
cla ss C ircle {
/ ** T he ra dius of t his circl e */
d oubl e rad ius = 1.0 ;
/ ** C onstr uct a cir cle objec t */
C ircl e() {
}
/ ** C onstr uct a cir cle objec t */
C ircl e(dou ble newRa dius ) {
rad ius = new Radiu s;
}
Eg Circle( ){ }
15
animation
myCircle
no value
Note:
myCircle is an object reference. It will be used to store
the address of a Circle object.
16
animation
myCircle
no value
: Circle
radius: 5.0
17
animation
myCircle
reference value
yourCircle.radius = 100;
Assign object reference to
myCircle
: Circle
radius: 5.0
Note:
The address of the newly created object (with radius 5.0)
is assigned to the object reference myCircle, i.e.
myCircle will now be pointing to the Circle object.
18
animation
reference value
: Circle
radius: 5.0
yourCircle
no value
Declare yourCircle
19
animation
: Circle
radius: 5.0
no value
yourCircle
: Circle
Create a new
Circle object
radius: 0.0
20
animation
myCircle
Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
: Circle
yourCircle.radius = 100;
radius: 5.0
yourCircle
Assign object
reference to
yourCircle
reference value
: Circle
radius: 1.0
21
animation
myCircle
Circle myCircle = new Circle(5.0);
Circle yourCircle = new Circle();
yourCircle.radius = 100;
: Circle
radius: 5.0
yourCircle
reference value
: Circle
Change radius in
yourCircle
radius: 100.0
22
4. Classes vs Objects
A
Members
Method
Members
Data member(s)
Name
I/C Number
Address
Balance
Method member(s)
Create new Customer
Deposit money
Withdraw money
Transfer money
23
Classes vs Objects
An object is an instance of
Tan Chee
193, Jln Big, KL
451021-9-6390
RM4066.67
Ali Mohd
23,
JlnHoe
A1, Ipoh
Tan
510215-6-5533
193, Jln Big, KL
RM456.98
950102-9-9990
RM32.00
5. Defining a Class
The general form of a class definition is show here:
public class <class name> {
//declare instance variables
type var1;
type var2
...
type varN
//declare methods
Method 1
Method 2
.
Method n
Data part
Method part
}
25
26
car2
29
is
null
0
0
0
30
Note
class Vehicle{
String plateNo;
int passengers;
If there is more than one
int fuelCap;
class in a file, only one
int mpg;
class can be a public }
class.
public class VehicleDemo{
public static void main(String args [ ]) {
The public class must
Vehicle car = new Vehicle();
have the same name as
car. plateNo = WWW 1234;
the file name.
car. Passengers = 4;
The main method must
car. fuelCap = 20;
car. mpg = 10;
be in a public class.
System.out.print(Plate No: +
car.plateNo );
9..
}
}
32
Class Methods
Methods are subroutines
What the class can do or
perform.
They provide access to and
manipulate the data
members of the class.
We will add a method called
range() to the Vehicle
class to return fuel
capacity * miles per
gallon.
class Vehicle {
String plateNo;
int passengers;
int fuelCap;
int mpg;
}
// Vehicle class definition
class Vehicle {
String plateNo;
int passengers;
int fuelCap;
int mpg;
int range() {
return fuelCap * mpg;
}
}
33
Constructors
A no-arg constructor
with an empty body is
implicitly declared if a
class is declared without
constructors.
Eg Vehcle( ){ }
This approach
would never be
used in
professionally
written Java code!
class Vehicle {
String plateNo;
int passengers;
int fuelCap;
int mpg;
int range() {
return fuelCap * mpg;
}
}
public class VehicleDemo {
public main(Sting args [ ]) {
Vehicle car = new Vehicle();
car. plateNo = WWW 1234;
car. Passengers = 4;
car. fuelCap = 20;
car. mpg = 10;
.
}
}
35
37
Class Constructors
We use constructors to
give initial values to the instances variables
defined by the class.
perform any other startup procedures required
to create a fully formed object.
38
Class Constructors
Constructors
may be
Constructors with
parameters
(parameterized
constructors)
No-argument / no-arg
constructors, e.g.
Vehicle() {
plateNo = ;
passenger = 0;
}
Default Constructors
class Vehicle{
String plateNo;
int passengers;
int fuelCap;
int mpg;
//this is a constructor for Vehicle
Vehicle (String No, int p, int f, int m){
plateNo = No;
passengers = p;
fuelCap = f;
mpg = m;
}
//return the range
int range(){ return fuelCap * mpg;
}
Reference
Main text:
Chapter 8