Professional Documents
Culture Documents
09 Aslide OOP
09 Aslide OOP
09 Aslide OOP
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Introduction to Object Oriented
Programming
Object oriented programming (OOP) is THE
programming methodology for designing complex
systems
OOP essentially is about the use of re-useable
software components called objects
These objects can be prebuilt by third party vendors
to ‘plug in’ to our application or can be developed
by ourselves
– Perhaps building on existing objects
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Introduction to Object Oriented
Programming
There are good reasons why building our application out
of such objects has major advantages
– We can abstract out the complex state and behaviour of
individual objects
– Only the interface between the objects in our
application is important
– We can use prebuilt objects such as Framework Class
Library (FCL) objects
We are able to extend these objects to meet our own
needs
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
OO Programming Concepts
Object-oriented programming (OOP) involves
programming using objects. An object represents
an entity in the real world that can be distinctly
identified. For example, a student, a desk, a circle,
a button, and even a loan can all be viewed as
objects. An object has a unique identity, state, and
behaviors. The state of an object consists of a set
of data fields (also known as properties) with their
current values. The behavior of an object is defined
by a set of methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved. 4
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Declaring/Creating Objects
The process of using a type to create an object is
called instantiation
In C#, C++, and Java the new keyword is used
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Objects
Class Name: Circle A class template
Data Fields:
radius is _______
Methods:
getArea
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
Classes
Classes are constructs that define objects of the
same type. A Java class uses variables to define
data fields and methods to define behaviors.
Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
SimpleCircle.java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
TestSimpleCircle.java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
TestSimpleCircle.java
Output:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
What is a Constructor ?
Use to Initialize data member when class
object is instantiated
Two types :
– Constructor without parameter – default
constructor
– Constructor with parameter
Overloading constructor
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
Constructors
Constructors are a special
Circle() { kind of methods that are
} invoked to construct objects.
Circle(double newRadius) {
radius = newRadius;
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
Constructors, cont.
A constructor with no parameters is referred to as
a no-arg constructor.
· Constructors must have the same name as the
class itself.
· Constructors do not have a return type—not
even void.
· Constructors are invoked using the new
operator when an object is created. Constructors
play the role of initializing objects.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Creating Objects Using
Constructors
new ClassName();
Example:
new Circle();
new Circle(5.0);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Default Constructor
A class may be defined without constructors. In
this case, a no-arg constructor with an empty body
is implicitly defined in the class. This constructor,
called a default constructor, is provided
automatically only if no constructors are explicitly
defined in the class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Example1: Declaring Constructor
public class UNIKL
{ Constructor name
private String name;
public UNIKL (String campus) Receiving parameter
{
name = campus; Store value of campus into name
}
}
public class UNIKLTest
{
public static void main(String args[ ])
{
UNIKL univ = new UNIKL(“miit");
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Example2: Declaring Constructor
public class Transportation
{
private String name;
Constructor name
private int year;
private String model;
No parameter
public Transportation( )
{
name=“Honda"; Store default value into data member
year=2021;
model=“Estima";
}
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
Example2: Declaring Constructor
Output:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
Example3: Declaring Constructor
public class Transportation
{
String name;
int year;
String model;
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24
Example2: Declaring Constructor
Output:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
Declaring Object Reference Variables
To reference an object, assign the object to a reference
variable.
ClassName objectRefVar;
Example:
Circle myCircle;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
26
Exercise
Class Author has 3 data members as String id,
String authorName and int yearPublished.
– Create a constructor without parameter (default
constructor) that will assigned a default value of
“123”,”Abu Bakar” and 2021 when an instance of
author1 is created.
– Create a constructor with parameter that will pass
default value of “123”,”Abu Bakar” and 2021 when an
instance of author1 is created through parameter.
Create the test class too !. Compile and execute
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
Exercise: Defining Classes and Creating Objects
TV
channel: int The current channel (1 to 120) of this TV.
volumeLevel: int The current volume level (1 to 7) of this TV.
on: boolean Indicates whether this TV is on/off.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28