0% found this document useful (0 votes)
0 views

Topic 1-3 Objects & Classes

The document provides an overview of Object-Oriented Programming (OOP), including its basic components, characteristics, and the software lifecycle stages. It defines key concepts such as objects, classes, attributes, behaviors, and methods, while illustrating these concepts with examples. Additionally, it covers OOP principles like abstraction, encapsulation, inheritance, and polymorphism.

Uploaded by

2024661568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Topic 1-3 Objects & Classes

The document provides an overview of Object-Oriented Programming (OOP), including its basic components, characteristics, and the software lifecycle stages. It defines key concepts such as objects, classes, attributes, behaviors, and methods, while illustrating these concepts with examples. Additionally, it covers OOP principles like abstraction, encapsulation, inheritance, and polymorphism.

Uploaded by

2024661568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL 1


LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

▪ Name the basic components of object-oriented programming


▪ Differentiate classes and objects.
▪ Name the elements of an object.
▪ Describe messages and methods (message passing).
▪ Describe the OOP characteristics.
▪ Name and explain the stages of the software lifecycle

CSC238 OBJECT ORIENTED PROGRAMMING 2


INTRODUCTION TO OBJECTS
DEFINITION OF OBJECT

▪ Object-oriented programs use objects.


▪ An object is a thing, both tangible and intangible.

TYPE EXAMPLE
Tangible (can be seen & touched) Vehicle, Employee, Clock
Intangible (conceptual) Account, Loan, Time, Date

▪ To create an object inside the computer program, we must provide


a definition for objects—how they behave and what kinds of
information they maintain —called a class.

▪ An object is an instance of class.

CSC238 OBJECT ORIENTED PROGRAMMING


INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT
▪ Each object contains data and operations to manipulate the data.
✓ operations are referred as methods or member functions
✓ data are referred as properties, attributes or member data.
▪ Elements of an object :

ELEMENT DEFINITION
Attribute Data associated with object
Behavior Action performed by an object (Operation)
Defined by methods
State The description (current value) of the attribute.

CSC238 OBJECT ORIENTED PROGRAMMING


INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT
For example, you want to write a program for video rental. The objects
involved are video and customer. This segment uses VIDEO as the object.
Object have three elements:

1. Attribute (data related to the object)


For the video object, the data or attributes might be movie name,
starring actors, producer name, production name and number of copies
in stock
2. Behaviour (operations on a video object)
For example, Checking the name of movie, reducing the number of
copies in stock by one after a copy is rented and incrementing the
number of copies in stock by one after a customer returns a copy.
3. State (current situation)

For the video object, the state might be rented, available and current
number of stock
INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT (Try this out ☺)

What are the attributes, states and behaviour of this object?


INTRODUCTION TO OBJECTS
ELEMENTS OF AN OBJECT
What are the attributes, states and behaviour of this object?

Attributes Behaviour
▪ Plat Number ▪ Move forward
▪ Model ▪ Reverse
▪ Color ▪ Stop
▪ Engine Size

States
▪ KCR 260
▪ Beetle
▪ Metallic Grey
▪ 2.0
INTRODUCTION TO CLASS
DEFINITION OF CLASS
▪ A class is a template, blueprint or contract that defines what
attributes (data fields) its object will have and what those objects will
be able to do (methods).
▪ A class is a collection of objects with common properties.

OBJECT CLASS
Apple
Mango FRUIT
Orange

✓ It is a user defined data type.


✓ Associate with each class is a set of properties (data) and
behaviors (functions).
✓ Objects are variables of classes.

CSC238 OBJECT ORIENTED PROGRAMMING


INTRODUCTION TO CLASS
DEFINITION OF CLASS

▪ A CLASS is an essentially a description of how to create an object

✓ A class must be defined before you can create an instance


(object) of the class.

✓ Creating an instance is referred to as instantiation.

CSC238 OBJECT ORIENTED PROGRAMMING


UNDERSTANDING OBJECTS & CLASSES

CAT (class) Breed : British Shorthair


Size: Small
Age: 2 years
Breed Color: Grey

Size
Age
Color (object) Breed : Bengal
Size: Medium
Age: 4 years
Eat( ) Color: Brown
Sleep( )
Sit( )
Run( )
Breed : Persian
Size: Large
Age: 3 years
Color: Dark Brown
UNDERSTANDING OBJECTS & CLASSES
OBJECT ORIENTED DESIGN (EXAMPLE 1)

Problem statement
▪ Write a program to input the length and width of a rectangle, and
calculate and print the perimeter and area of the rectangle

Nouns:
✓ length
✓ width
✓ rectangle
✓ perimeter
✓ area

CSC238 OBJECT ORIENTED PROGRAMMING


UNDERSTANDING OBJECTS & CLASSES
OBJECT ORIENTED DESIGN (EXAMPLE 1)
Class Rectangle with 2 data members and 7 operations

CSC238 OBJECT ORIENTED PROGRAMMING


UNDERSTANDING OBJECTS & CLASSES
OBJECT ORIENTED DESIGN (EXAMPLE 2)

CSC238 OBJECT ORIENTED PROGRAMMING


UNDERSTANDING OBJECTS & CLASSES
UML CLASS DIAGRAM (EXAMPLE 2)

CSC238 OBJECT ORIENTED PROGRAMMING


UNDERSTANDING OBJECTS & CLASSES
CLASS vs. OBJECT

CLASS OBJECT
A class is a template for An object is a member or an
objects. "instance" of a class.
A class defines object An object has a state in
properties including a valid which all of its properties
range of values, and a have values that you either
default value. explicitly define or that are
defined by default settings.
A class also describes object
behavior.

CSC238 OBJECT ORIENTED PROGRAMMING


MESSAGES & METHODS

▪ To instruct a class or an object to perform a task, we send a


message to it.

▪ You can send a message only to the classes and objects that
understand the message you sent to them.

▪ Calling a specific method in an object is described as sending


the object a message.

▪ A class or an object must possess a matching method to be able


to handle the received message.
CSC238 OBJECT ORIENTED PROGRAMMING
MESSAGES & METHODS
▪ For example if “CUSTOMER” and “ACCOUNT” are two objects in a
program, then the customer object may send a message to the
account object requesting for the bank balance.

✓ A method defined for a class is called a CLASS METHOD, and


a method defined for an object is called an INSTANCE
METHOD.

✓ A value we pass to an object when sending a message is


called an ARGUMENT of the message.

CSC238 OBJECT ORIENTED PROGRAMMING


MESSAGES & METHODS
MESSAGE PASSING (Customer & Account)

Messages enter PIN NUMBER

verify

request withdrawal

dispense money

CSC238 OBJECT ORIENTED PROGRAMMING


EXERCISE 1

The Center of Islamic Kids wants to automate the


process of storing student’s information and the fee
billing system. You have been given the task of
designing the system. The center normally stores
information such as the student’s name, date of birth,
parent’s name, parent’s contact number, address, and
medical data. As for fee, it is determined by either the
kid is sent for a half‐day or full‐day session and either
food services are chosen or not.

✓ As the first step in the design process, identify the


objects and their attributes for the system. (DO NOT
write a class for this problem)

CSC238 OBJECT ORIENTED PROGRAMMING


EXERCISE 2

The following is the situation of football club at UiTM


Raub. They plan to store and automate information
related to the club. You were assigned to design the
system. The information is related to teams and players
for an every competition they involved. Every single
team can have many players. The information consists
in team is the id as well the team name. However a
player can only play one team at any one time. These
players can play for many matches. Provide the
players details such as their id, name and position id
(every position belongs to a player).

✓ As the first step in the design process, identify the


objects and their attributes for the system. (DO NOT
write a class for this problem)
CSC238 OBJECT ORIENTED PROGRAMMING
OOP CHARACTERISTICS

ABSTRACTION ENCAPSULATION
/DATA HIDING

POLYMORPHISM

INHERITANCE
OOP CHARACTERISTICS
ABSTRACTION

Definition
Process of taking away or removing characteristics from an
object in order to reduce it to a set of essential characteristics
(attributes and behavior of an object).

▪ Abstraction in Java allows the user to hide non essential details


relevant to user in order to reduce complexity and increase
efficiency
✓ It allows only to show the essential features of the object to the
end user.
✓ A user’s code should not access the implementation details of
the class used.
✓ In other sense we can say it deals with the outside view of an
object (interface).
OOP CHARACTERISTICS
ABSTRACTION

▪ The result of doing abstraction can also be called an


abstraction meaning a named entity made up of selected
attributes and behavior specific to an object.

▪ See http://whatis.techtarget.com/definition/abstraction
OOP CHARACTERISTICS
ENCAPSULATION (DATA HIDING)

Definition

Binding or wrapping‐up of data and associated methods into


a single unit (class)

▪ The data are not accessible to the outside world and only
those methods, which are wrapped in the class, can access
it – provide privacy to data and some of the methods

▪ Also known as data hiding.


OOP CHARACTERISTICS
INHERITANCE

Definition
Inheritance is a mechanism in OOP to design two or more
entities that are different but share many common features.

▪ The capability of a class to use the properties and methods of


another class while adding its own functionality.
– The new class will have the features of the old class.
- “is-a” relationship
- Concept of reusability
▪ Features common to all classes are defined in the superclass
(ancestor).
▪ The classes that inherit common features from the superclass
are called subclasses (descendant).
OOP CHARACTERISTICS
INHERITANCE (EXAMPLE)

Here are the superclass Account and its subclasses Savings and
Current.

Account

Current Savings
OOP CHARACTERISTICS
INHERITANCE (EXAMPLE)

An example of inheritance hierarchy among different types of


students.

Student

Graduate Undergrad

Masters Doctoral Law Commuting Resident


OOP CHARACTERISTICS
INHERITANCE (EXAMPLE)

An hierarchy of bicycle classes


OOP CHARACTERISTICS
POLYMORPHISM

Definition
The ability of an object to take on many forms
✓ the capability of an action or method to do different things
based on the object that it is acting upon
✓ Allows programmer to send same message to objects from
different classes.
✓ Allows a single variable to refer objects from different
objects from different subclasses in the same inheritance
hierarchy.
OOP CHARACTERISTICS
POLYMORPHISM (EXAMPLE 1)

Example:

Given a superclass shape, polymorphism enables the user


to define different area methods for any number of subclasses,
such as circles, rectangles and triangles. No matter what
shape an object is, applying the area method to it will return
the correct results.
OOP CHARACTERISTICS
POLYMORPHISM (EXAMPLE 1)

SHAPES & ITS SUBCLASSES


OOP CHARACTERISTICS
POLYMORPHISM (EXAMPLE 2)

MONSTER & ITS SUBCLASSES


SOFTWARE ENGINEERING
SOFTWARE LIFE CYCLE

▪ Software engineering is the application of a systematic and


disciplined approach to the development, testing, and
maintenance of a program.
▪ In this class, we will learn how to apply software engineering
principles when we develop sample programs.
▪ The sequence of stages from conception to operation of a
program is called software life cycle.
▪ Five stages of Software Life Cycle are :
✓ Analysis
✓ Design
✓ Coding
✓ Testing
✓ Operation and Maintenance
THANK YOU

CSC238 OBJECT ORIENTED PROGRAMMING 34

You might also like