Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

CHAPTER 1

Introduction to Object-Oriented
Programming (OOP)
Lecture by Ts. Hanis Basira Abu Hasan
27th September 2022

Jabatan Teknologi Maklumat Dan Komunikasi


Course Learning Outcome (CLO)
Upon completion of this chapter, student should be able to:

• CLO1 : Differentiate various programming techniques that exist.

• CLO2: Describe term used in object oriented analysis and design.

• CLO3: Describe object, attribute and behavior.

• CLO4: Explain the object oriented concepts and terminologies.

2
Sub-Chapter 1.1

1.1 Describe the


programming techniques

3
Learning Contents Sub-Chapter 1.1

1.1 Describe the programming techniques.


1.1.1 Differentiate various programming techniques that
exist:
a. Unstructured Programming
b. Procedural Programming
c. Object Oriented Programming
1.1.2 Describe object oriented approach.
1.1.3 Identify the benefits of using the OOP approach.
1.1.4 Describe the terms used in object oriented analysis and
design (OOAD):
a. Object-Oriented Analysis (OOA)
b. Object-Oriented Design (OOD)
c. Object-Oriented Programming (OOP)
4
Various Programming
Techniques

5
Various Programming Techniques
Unstructured Programming
4

• A programming language in which the entire logic of the


program is written as a single continuous (nonstop or
unbroken) block is called "unstructured Programming".
Program written by following this language is very difficult to
modify and to debug.

• This programming consist of just writing the sequence of


commands or statements in the main program which
modified the state maintained in global data.

• Example Language: Assembly Language

6
Various Programming Techniques
Unstructured Programming

Program to compute 2 * 4 using assembly language


7
Various Programming Techniques
Unstructured Programming
Limitation of unstructured programming are:

The data is global and code


operates on it

As the size of code increase,


maintenance is a problem

Does not have independent


data for processing

Reusability of code not


supported
8
Various Programming Techniques
Procedural Programming
5

• Procedural programming is a programming paradigm,


derived from structured programming, based on the concept
of the procedure call. Procedures, also known as routines,
subroutines, or functions, simply contain a series of
computational steps to be carried out.

• During a program’s execution, any given procedure might be


called at any point, including by other procedures or itself.

• Procedural programming is useful to bring order in maze of


algorithms available to solve a particular problem.

• Example Language: FORTRAN, ALGOL, COBOL, BASIC,


Pascal and C, C++.
9
Various Programming Techniques
Procedural Programming
5
Limitation of procedural programming are:

As the code size grows large, maintaining the code


becomes difficult

Any major changes required to the functionality may


result in a lot of changes in code

Code is not close to real world scenario

Preventing accidental modification of data is limited

10
Various Programming Techniques
Object Oriented Programming
7

• Object oriented programming can be defined as a programming model


which is based upon the concept of objects. Objects contain data in the
form of attributes and code in the form of methods.

• In object oriented programming, computer programs are designed using


the concept of objects that interact with real world.

• Object oriented programming languages are various but the most popular
ones are class-based, meaning that objects are instances of classes, which
also determine their types.

• Example Language Java, C++, C#, Python, PHP, JavaScript,


Ruby, Perl, Objective-C, Dart, Swift,
Scala.

11
Various Programming Techniques
Object Oriented Programming
7

Object oriented programming using phyton

12
Unstructured Programming Vs Procedural
Programming Vs Object Oriented Programming
9

• Statements are executed in sequence


• Often difficult to understand the logic
Unstructured

• Executes a set of commands in order.


• Practice top down approach.
Procedural • Based on procedure calls. Statements organized into procedures
known as functions.

• Based on ‘objects’.
Object-
• Gives more preference to data rather than logic
Oriented • Bottom up approach

13
Unstructured Programming Vs Procedural
Programming Vs Object Oriented Programming
10

Source: http://www.differencebetween.info/difference-between-procedural-structural-and-object-
oriented- programming-languages
14
Object Oriented
Approach

15
Object Oriented Approach
OOP Concept

• Object means a real word entity such as pen, chair, table etc.

16
Object Oriented Approach
OOP Concept

• Object-Oriented Programming (OOP) is a methodology or


paradigm to design a program using classes and objects. It
simplifies the software development and maintenance by
providing some concepts:
a. Object
b. Class
c. Inheritance
d. Polymorphism
e. Abstraction
f. Encapsulation

17
Object Oriented Approach
History of OOP

SIMULA was the first object language. As its name suggests it was used to create
simulations.

Smalltalk was the object-oriented language developed for programming the


Dynabook. It was a simulation and graphics-oriented programming language

The idea of object-oriented programming gained momentum in the 1970s and in


the early 1980s Bjorn Stroustrup integrated object-oriented programming into the
C language. The resulting language was called C++ and it became the first OOP
language to be widely used commercially.

In the early 1990s a group at Sun led by James Gosling developed a simpler version of
C++ called Java that was meant to be a programming language for video-on-demand
applications. The language has gained widespread popularity as the Internet has
boomed

18
Benefit of Using Object Oriented
Approach

19
Benefit of Using Object Oriented Approach
Advantages of OOP

Simple - Software objects are treated similar to real


objects.

Data Security - Each object can access only the


essential members of the object of the other class.

Reusability - Programs can be assembled from


pre-written software components.

Modularity - large software projects can be split up in


smaller pieces
20
Terms Used

21
Terms Used
Object Oriented Analysis And Design (OOAD)

• OOAD stand for Object-oriented Analysis and Design

o Analysis — understanding, finding and describing


concepts in the problem domain.

o Design — understanding and defining software


solution/objects that represent the analysis concepts and
will eventually be implemented in code.

o OOAD —A software development approach that


emphasizes a logical solution based on objects.
22
Terms Used
Object Oriented Analysis And Design (OOAD)

• OOAD is the generic term for the process of analysing a


problem and developing an approach for solving it.

• It is suitable for big problem.

• The methods of OOAD become more appropriate than


pseudocode as problem and the groups of people solving
them increase in size.

23
Terms Used
Object Oriented Analysis And Design (OOAD)
• The following represents the terms used in Object Oriented
Analysis and Design (OOAD)

OBJECT ORIENTED OBJECT ORIENTED OBJECT ORIENTED


ANALYSIS DESIGN PROGRAMMING

• This is an analysis • This represents the • This represents the


method in which the process of object implementation of
requirement are oriented approach object oriented
mapped to the which also provides a concepts in terms of a
perspective of classes notation for depicting co-operative
and objects suiting the the system under collection of objects.
domain of the design These in turn are
requirements instances of classes.
The classes in turn
may form a hierarchy.

24
Sub-Chapter 1.2

1.2 Explain the Object


Oriented concepts and
terminologies

25
Learning Contents Sub-Chapter 1.2

1.2 Explain the Object Oriented concepts and terminologies.


1.2.1 State an example of object oriented languages.
1.2.2 Describe object, attribute and behaviour.
1.2.3 Explain the object oriented concepts and terminologies:
a. Class
b. Object
c. Encapsulation
d. Inheritance
e. Data Abstraction
f. Polymorphism

26
Object Oriented
Languages

27
Object Oriented Languages
Examples

• Simula, the first object-oriented programming language


• Java
• Python
• Ruby
• C++
• Smalltalk
• Visual Basic .NET
• Objective-C: OOP is a core tenet of iOS mobile app programming, and
Objective-C is essentially the C language with an object-oriented layer.
• Curl
• Delphi
• Eiffel

28
Object Oriented Languages
Top OOP Languages To Follow In 2020

1 Python

2 Java

3 C#

4 Ruby

5 C++

29
Object, Attribute And Behaviour

• Classes and objects are building blocks of OOP approach.


 Class - A class is a template or blueprint to create an
object.
 Object - Is an instance of a class.
 Attributes - Characteristics of real world object; also
known as data members in a class, variables or data field.
 Behaviour of objects are called as methods or member
functions of a class.

30
Object, Attribute And Behaviour

Object

<<instanceOf>> Class Car

Object 1 State / Attribute


<<instanceOf>>  Model
 Location
Object 2  #Wheels = 4
<<instanceOf>>
Behavior / Method
Object 3 Start
 Accelerate
Reverse
31
Object, Attribute And Behaviour

BankAccount

Holder Class Name Account


- Holder_ID - Account_ID
- FirstName - Number
- LastName Attributes - Balance
- Address
- Profession
- Birthday
o ChangeResidence() : o Deposit() : void
void (Behavior) o Withdrawal() : void
o ChangeProfession() : o CheckBalance() :
void float

32
Object Oriented Concepts And
Terminologies

33
Object Oriented Concepts And Terminologies
Basic Terminologies of OOP

Classes

Polymorphism Object

OOP

Inheritance Encapsulation

Data
Abstraction
34
Object Oriented Concepts And Terminologies
Objects

• Objects are key to understanding object-oriented technology.

• Look around right now and you'll find many examples of real-
world objects: your dog, your desk, your television set, your
bicycle.

• Real-world objects share two characteristics:


state and behavior.

35
Object Oriented Concepts And Terminologies
Objects

• Example : class Bicycles

• Bicycles have
– state (current gear,
current pedal cadence,
current speed) and
– behavior (changing
gear, changing pedal
cadence, applying
brakes)

36
Object Oriented Concepts And Terminologies
Objects
• An object
– stores its state in fields (variables in some programming
languages) and
– exposes its behavior through methods (functions in some
programming languages).

37
Object Oriented Concepts And Terminologies
Classes
• Class is a blueprint or prototype from which objects are created.

• Class is a blueprint or prototype defining the variables and


methods common to all objects of a certain kind.

• For example:
– There may be thousands of other bicycles in existence, all of
the same made and model. Each bicycle was built from the
same set of blueprints and therefore contains the same
components.

• In object-oriented terms, we say that your bicycle is


an instance of the class of objects known as bicycles. 38
Object Oriented Concepts And Terminologies
Classes

Class Bicycle

State / Attribute
 color
 speed
 gear = 1

Behavior / Method
setColor
 changeGear
 speedup
printStates

39
Object Oriented Concepts And Terminologies
Classes – Example Bicycle
Bicycle.java Output
BicycleDemo.java

40
Object Oriented Concepts And Terminologies
Class and Object

41
Object Oriented Concepts And Terminologies
Encapsulation
• Binding (or wrapping) code and data together into a single unit
is known as encapsulation.

• For example: Capsule, it is wrapped with different medicines.

• A java class is the example of encapsulation. Java bean is the


fully encapsulated class because all the data members are
private here.

42
Object Oriented Concepts And Terminologies
Encapsulation - Example Encapsulation
Encapsulation.java Output

EncapsulationTest.java

43
Object Oriented Concepts And Terminologies
Inheritance
• OOP allows classes to inherit commonly used state and
behavior from other classes.

• In this example, Bicycle now becomes the superclass of


MountainBike, RoadBike, and TandemBike.

• In the Java programming language,


– Each class is allowed to have one direct superclass, and
– Each superclass has the potential for an unlimited number of
subclasses.

44
Object Oriented Concepts And Terminologies
Inheritance
• Inheritance provides a powerful and natural mechanism for organizing
and structuring your software.

• Different kinds of objects often have a certain amount in common


with each other.

• For example: Mountain bikes, road bikes, and tandem bikes; all share
the characteristics of bicycles (current speed, current pedal cadence,
current gear).

• Yet each also defines additional features that make them different:
– Tandem bicycles have two seats and two sets of handlebars;
– Road bikes have drop handlebars;
– Mountain bikes have an additional chain ring, giving them a lower
gear ratio.
45
Object Oriented Concepts And Terminologies
Inheritance
• The syntax for creating a subclass
is simple. At the beginning of
your class declaration, use the
extends keyword, followed by
the name of the class to inherit
from:

class MountainBike extends


Bicycle {
// new fields and methods
defining
// a mountain bike would
go here
}

https://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html 46
Object Oriented Concepts And Terminologies
Inheritance
• Another example:
– Classes can be organized into hierarchies.

Account

Charge Bank
Account Account

Savings Checking
Account Account

47
Object Oriented Concepts And Terminologies
Polymorphism

• Polymorphism refers to a principle in biology in which an


organism or species can have many different forms or stages.

• Subclasses of a class can define their own unique behaviors and


yet share some of the same functionality of the parent class.

48
Object Oriented Concepts And Terminologies
Polymorphism - Example Bicycle
Bicycle.java

49
Object Oriented Concepts And Terminologies
Polymorphism - Example Bicycle
Bicycle.java

50
Object Oriented Concepts And Terminologies
Polymorphism - Example Bicycle

MountainBike.java

51
Object Oriented Concepts And Terminologies
Polymorphism - Example Bicycle

RoadBike.java

52
Object Oriented Concepts And Terminologies
Polymorphism - Example Bicycle
TestBikes.java

Output

53
Object Oriented Concepts And Terminologies
Data Abstraction

• Hiding internal details and showing functionality is known as


abstraction.

• For example: Phone call, we don't know the internal processing.

• In java, we use abstract class and interface to achieve


abstraction. It’s cannot be initiated.

• Abstract class is a class that is declared using “abstract” keyword


is known as abstract class.

54
Object Oriented Concepts And Terminologies
Data Abstraction – Example Demo1
Demo2.java

Output

55
Object Oriented Concepts And Terminologies
Distinguish Between Abstraction & Encapsulation
• Abstraction represent taking out the behavior from How exactly its
implemented, example of abstraction is interface.

• Encapsulation means hiding details of implementation from


outside world so that when things change no body gets affected.
Example of Encapsulation in Java is private methods.

a. Abstraction is implemented in Java using interface and


abstract class while Encapsulation is implemented using
private, package-private and protected access modifier.

b. Encapsulation is also called data hiding.

c. Design principles "programming for interface than


implementation" is based on abstraction and "encapsulate
whatever changes" is based upon Encapsulation.
56
Introduction to Object Oriented Programming
Lab1 – Chapter 1 (Activity 1)

Activity 1A until Activity 1E


(Individual Tasks)

57
~THE END~

End Of Chapter 1

58

You might also like