F3031
Object Oriented Programming
CHAPTER 1
INTRODUCTION TO
OBJECT ORIENTED PROGRAMMING
(OOP)
By:
PN. NOR ANISAH BINTI MOHD SAAD
CHAPTER 1.0 : INTRODUCTION TO OOP
1
Define OOP
2
History of OOP
3
Advantages of using OOP
4
Basic Terminologies of OOP
5
Abstraction and Encapsulation
6
Structured and OOP Approach
1 DEFINE OBJECT
An object can be considered as a "thing" that
can perform a set of activities (behaviours).
For example, a "Student" object can tell you its
name or its address.
DEFINE OBJECT
An object is a "black box" which receives and sends
messages.
A black box actually contains code and data
Messages define the interface to the object.
Everything an object can do is represented by its message
interface.
So you shouldn't have to know anything about what is in
the black box in order to use it.
1 DEFINE OBJECT ORIENTED PROGRAMMING
In object-oriented programming, objects interact
with each other by messages (specific codes).
The only thing that an object knows about another
object is the object's interface. Each object's data
and method is hidden from other objects.
In other words, the interface encapsulates the
object's code and data.
1 DEFINE OOP
A type of programming where the programmer can
define data types and types of operation
(function), which will be performed on the data
structure.
Data
Matric No.
Marks
Types of
Data Type
operation
Integer Sorting students
Float Calculate marks
1 DEFINE OOP (CONT.)
Object-oriented programming is a method used to
write programs where data and behaviours are
packaged together in one class. Object is a member
of a class.
Object Oriented Programming is programming
which is oriented around objects, thus taking
advantage of Encapsulation, Polymorphism, and
Inheritance to increase code reuse and decrease
code maintenance.
1 DEFINE OOP (CONT.)
A language or technique is object-oriented if it
directly supports:
Abstraction - providing some form of classes and
objects.
Inheritance - providing the ability to build new
abstractions out of existing ones.
Runtime polymorphism - providing some form of
runtime binding.
2 HISTORY OF OOP
Early 1960s
Kristen Ole-Johan
Nygaard Dahl
SIMULA was the first object language.
developed by Kristen Nygaard and Ole-Johan Dahl in
Norway.
It was designed for the purpose of creating
simulations (ships)
2 HISTORY OF OOP
1970s
Alan Kay
Alan Kay – idea to integrated the SIMULA into personal computer
Joint venture with Xerox Parc – created the first personal
computer called Dynabook
Smalltalk was the object-oriented language developed for
programming the Dynabook
It was a simulation and graphics-oriented programming language.
2 HISTORY OF OOP
1980s
Bjorn Stroustrup
Bjorn Stroustrup integrated object-oriented
programming into the C language
The resulting language was called C++ and it
became the first object-oriented language to be
widely used commercially
2 HISTORY OF OOP
1990s
James Gosling
a group at Sun led by James Gosling developed a simpler
version of C++ called Java
Java programming meant to be a programming language
for video-on-demand applications
No +ve response - re-oriented its focus and marketed
Java as a language for programming Internet applications
The language has gained widespread popularity as the
Internet has boomed
3 ADVANTAGES OF USING OOP
Code extensibility
Ability to add codes, which are already existing and consistent with
needs.
Code reusability
Existing code can be tested first before it is inherited from previous
module.
This will save cost and time needed to develop software.
With this method, it will help to increase programmer’s productivity.
Represent real world
Using object-oriented concept, it will assist the programmer in creating
a module because data used represents real situations.
Data security
The use of encapsulation concept has made data to be protected
from misuse by an unauthorized person.
4 BASIC TERMINOLOGIES OF OOP
1 Object
2 Class
3 Encapsulation
4 Data Abstraction
5 Inheritance
6 Polymorphism
4 BASIC TERMINOLOGIES OF OOP (CONT.)
1 Object
Object is the term used to explain many things.
Example: student, chair and circle.
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.
4 BASIC TERMINOLOGIES OF OOP (CONT.)
2 Class
A set of objects that have similar attributes (data) and methods.
Attributes and methods of a class can be used by each object
from that class.
class Student
{ String name, address, status;
int icno, sid; data
double marks;
char DetermineStatus()
{
if marks >= 40 method
status = “Pass”;
else
status = “Fail”;
}
} Class
4 BASIC TERMINOLOGIES OF OOP (CONT.)
3 Encapsulation
Encapsulation is a process of tying together all data and
methods that form a class and control the access to data by
hiding its information.
It enables access to object just by using methods of that object.
It is one of the security features in OOP.
Attributes and methods of a class can be used by each object
from that class.
Class Student
Name, Student ID, Address, IC No
Calculate_result()
Determine_grade()
Print_result()
4 BASIC TERMINOLOGIES OF OOP (CONT.)
4 Data Abstraction
Data abstraction is a process to delete all unnecessary
attributes and remain the necessary attributes to describe an
object
Attributes and methods of a class can be used by each object
from that class.
Class Box
Characteristics
Data abstraction
length Object Length, width, depth
Box Behaviors
depth
width Calculate_Volume()
Calculate_Area()
Figure 1.2: Describe data abstraction concept in OOP for object Box.
4 BASIC TERMINOLOGIES OF OOP (CONT.)
5 Inheritance
Create a new class from an existing class together with new
attributes and behaviours.
The new class will have the same ability as the base class.
Use the concept of code reusability.
Class A Base class for B
Derived class from class A Base class
Class B
for class C, D and E
Class C Class D Class E
Derived class from class B
4 BASIC TERMINOLOGIES OF OOP (CONT.)
6 Polymorphism
Polymorphism is a process of giving the same message to
two or more different objects and produce different behaviours
depending on how the objects receive the message.
It is said to be “One interface, many methods”.
Contoh:
Method:
Keluarkan wang anda dari bank:
Objek:
Pelajar 1 : Menggunakan mesin ATM yang disediakan oleh pihak
bank di mana akaun dibuka
Pelajar 2 : Menggunakan mesin ATM (MEPS) dari bank lain
6 STRUCTURED AND OOP APPROACH
Pengaturcaraan Berstruktur OOP
Berasaskan Fungsi Yang didefinisikan Teknik Pengaturcaraan Berasaskan
1
Pengaturcara Objek
Memecahkan satu program yang besar Penggunaan konsep pengkapsulan
pada beberapa fungsi. Setiap fungsi yang menggabungkan data dan fungsi
akan melaksanakan tugas yang lebih dalam satu komponen (kelas)
spesifik Contoh:
Contoh: class Individu{
void main( ){ ……….. // data
……….. void DataPeribadi( )
Peribadi (nama,umur); };
} main( ){
void Peribadi(char *a, int b) Individu a;
{ //Definisi fungsi DataPeribadi } /*Objek a capai data dan method
dari kelas Individu*/
}
6 STRUCTURED AND OOP APPROACH (CONT.)
Kod aturcara tidak boleh Membenarkan penggunaan kod yang
2
digunakan berulangkali berulang kali
Setiap fungsi ditugaskan untuk satu Ia boleh dilakukan melalui teknik
tugas yang spesifik. Kita mesti pewarisan.
menerima fungsi itu sebagaimana ia Teknik ini membolehkan objek untuk
ditulis. mewarisi ciri-ciri (fungsi dan data) objek
Untuk mengubahsuainya, kod itu lain.
haruslah disalin semula dan diubah
untuk memenuhi keperluan. Contoh:
Kita menggunakan radio kegunaan rumah
Contoh: yang telah dicipta untuk digunakan pada
Kita telah merekacipta satu radio kereta dan di pantai
untuk kegunaan rumah.
Teknologi yang digunakan itu boleh
digunakan untuk mencipta radio
kereta ataupun radio yang digunakan
di tepi pantai.
6 STRUCTURED AND OOP APPROACH (CONT.)
Fungsi Memanipulasikan Data Menggunakan Pengkapsulan Untuk
3
Data (cth: nama, umur) akan dihantar Bertindak Ke atas Data
ke fungsi-fungsi tertentu (cth: cetak, Pengkapsulan digunakan untuk
papar, tambah, dan padam) untuk mempakej data bersama fungsi yang
dimanipulasikan. Fungsi yang akan akan bertindak ke atas data. Ia
memanipulasikan data tidak mengenalpasti fungsi yang akan
ditetapkan terlebih dahulu. dilaksanakan ke atas setiap objek. Kelas
akan mengawal sebarang operasi keatas
data dan fungsi yang berada didalamnya.
Tiada Kawalan Capaian Data Kekangan Dalam Mencapai Data
4
Fungsi main ( ) boleh capai semua Wujudnya kawalan capaian terhadap
data dan fungsi yang terdapat pada data di dalam kelas. Contohnya
program katakunci private dan protected. Untuk
mencapai private data ia mesti dilakukan
melalui method ataupun mekanisma
pengkapsulan.
Tidak menyokong polimorfisma Penggunaan Polimorfisma
5
Setiap data mesti diisytiharkan (polymorphism)
terlebih dahulu sebelum operasi ke Polimorfisma membolehkan satu fungsi
atasnya dilaksanakan. dilaksanakan dengan pelbagai kaedah.