0% found this document useful (0 votes)
60 views39 pages

CSC186 - 1) Introduction To OOP

Uploaded by

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

CSC186 - 1) Introduction To OOP

Uploaded by

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

CSC186

OBJECT ORIENTED
PROGRAMMING
Topic 1:
Introduction to OOP
Topic Covered

Programming basics
Introduction to objects & classes
Elements of an object - attribute, behavior,
state
Characteristics of OOP - abstraction (basic
concept, process abstraction, data abstraction),
encapsulation, inheritance, polymorphism
STRUCTURED
PROGRAMMING APPROACH
Structured Programming Approach

◦ Is a technique for organizing and coding programs.

◦ A method of writing a computer program that uses


1. Top-down analysis for problem solving.
2. Modularization for program structure and organization.
3. Structured code for the individual modules (organize within various control
structures).

◦ It's breaking down a program into subprograms (modularization / functions).


OBJECT-ORIENTED
PROGRAMMING APPROACH
Object-Oriented Programming Approach

◦ Is a method that involves the use of objects and their relationships in order to
describe, programmatically, the problem to be solved.

◦ It represents concepts as "objects" that have data members (attributes that


describe the object) and associated procedures known as methods (behaviours
for the object).

◦ Each object has attributes, behaviors, and states.

◦ The basic concepts of OOP are Abstraction, Encapsulation, Inheritance and


Polymorphism.
Object-Oriented Programming Approach

• Objects have both data and methods


• Objects of the same class have the same data elements and
methods
• Objects send and receive messages to invoke actions

◦ Key idea in object-oriented:

The real world can be accurately described as a collection of


objects that interact.
7
Why OOP?

• Save development time (and cost) by reusing code


◦once an object class is created it can be used in other applications

• Easier debugging
◦classes can be tested independently
◦reused objects have already been tested

8
INTRODUCTION TO OBJECTS
Introduction to Object
◦ Objects are key to understanding object-oriented
technology.

◦ In real life, things that you see such as cars, trees, cats,
mobile phones and so on are objects. Even, you as a
student is an object.

◦ Each object has attribute/states, behaviors and identity.


ELEMENTS OF AN OBJECT
Elements of Object
Object can perform actions and can have actions
performed on it

Object can be
called and used as
Behavior single unit
Properties used to
defined
characteristics of Attribute /
the object Identity
State

OBJEC
T
OBJECT-ORIENTED
PROGRAMMING TERMS
Class & Objects

class
- a category of similar objects (a group)
- does not hold any values of the object’s attributes
object
- usually a person, place or thing (a noun)
attribute
- description of objects in a class (a characteristic)
method
- an action performed by an object (a verb)
Class & Objects
◦ Each object has:
◦ Attribute/Field - descriptive characteristics
◦ Behavior/Method - what it can do (or what can be done
to it)

E.g:
◦ The attribute of a bank account includes its account number and its current
balance
◦ The behaviors associated with a bank account include the ability to make
deposits and withdrawals
Example for attributes and methods

Attributes Methods

◦ manufacturer’s name ◦ Define data items (specify


◦ model name manufacturer’s name, model,
◦ year made year, etc.)
◦ color
◦ Change a data item (color,
engine, etc.)
◦ number of doors ◦ Display data items
◦ size of engine ◦ Calculate cost
◦ etc. ◦ etc.
Example:
class
Student Attributes
-name ◦ Represents the data / attributes variables.
-id ◦ A set of properties
+setName() ◦ Known as fields / attributes
+setId()

Methods
stud : Student • Represents the behaviors.
name=“Orked” • A sequence of instructions that a class or an object
id=“1234” follows to perform a task.
+setName()
+setId() object / instance of the class
Class Object
 a kind of template.  a thing, both tangible and
 represents the intangible.
common structure  is comprised of data &
behavior shared by operations that
the same type. manipulate these data.
 a collection of  is called an instance of a
objects of similar class.
type.
 must be defined
before creating an
instance of the class.
Test Yourself
Dreamer Dance Class is an institution that conducts
dance classes for its clients. The institution would
like to upgrade their client registration system. For
each client registered, the new programs will store
client's name, address and phone number; the name
of dance classes in which the client is enrolled.
Among the information given, identify the object
and the attributes for the system.
CHARACTERISTICS OF OOP
Characteristics/concepts of OOP

1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
ABSTRACTION
ABSTRACTION
◦ Focus only on the important facts about the
problem at hand

◦ To design, produce, and describe so that it can be


easily used without knowing the details of how it
works.

◦ The act of representing essential features without


including the background details or explanations.
ABSTRACTION

Analogy:

◦ When you drive a car, you don’t have to know


how the gasoline and air are mixed and ignited.

◦ Instead you only have to know how to use the


controls.
ENCAPSULATION
ENCAPSULATION

• The mechanism that binds together code and the


data it manipulates and keeps both safe from the
outside interference and misuse.

• Access to the code & data inside the wrapper is


tightly controlled through a well-defined interface.

26
ENCAPSULATION

◦ Encapsulation is restricting a user to access control of a particular process.

◦ It provides safety and ensures system robustness.

◦ Data hiding - Provides a way to protect your data from the outside world.
ENCAPSULATION

Analogy:

◦ Power steering mechanism of a car.

◦ Power steering of a car is a complex system

◦ Internally have lots of components tightly coupled


together

◦ External world there is only one interface is available


and rest of the complexity is hidden.
INHERITANCE
INHERITANCE
◦ The process by which one object acquires the properties of another object.

◦ An object inherits general attributes from its parent.

◦ A subclass has at least one attribute/method that differs from its superclass

◦ Other names :
◦ base class-derived class
◦ parent class-child class
INHERITANCE

◦ Superclass – inherit its attributes & methods to the


subclass(es).

◦ Subclass – can inherit all its superclass attributes &


methods besides having its own unique attributes &
methods.
An Inheritance Hierarchy

Superclass
Vehicle
Subclasses

Automobile Motorcycle Bus

Sedan Sports Car Luxury Bus School Bus

What properties does each vehicle inherit from the types of vehicles above it
in the diagram?

32
Example : mobile phone
CameraPhone

model
manufacturer
price
pixel PdaPhone
MobilePhone
… model
model
manufacturer manufacturer
price subclass price
memoryCap

… …

superclass subclass
INHERITANCE

Analogy:

◦ Car is a classification of Four Wheeler.

◦ Here Car acquires the properties of a four-wheeler.

◦ Other classifications could be a jeep, tempo, van etc.

◦ Four Wheeler defines a class of vehicles that have four wheels.


POLYMORPHISM
POLYMORPHISM
• From the Greek, meaning “many forms”.

• A feature that allows one interface to be used for a general


class of actions.

• “One interface, multiple methods”


Superclass : Shape
• E.g : Subclasses : Rectangle, Square

Shape sh = new Shape[2];


sh [0] = new Rectangle();
sh [1] = new Square();
POLYMORPHISM
• Can be applied in the overloaded methods (a few methods
that have the same name but with different parameters).

E.g:

◦ Super Class : Shape


◦ Variables : width, height
◦ Methods : ….
displayShape(char simbol)
displayShape(int h, int w)

This class has 2 methods with the same name but with different type of parameters
POLYMORPHISM

Analogy:

◦ A car have a gear transmission system.

◦ It has four front gears and one backward gear.

◦ When the engine is accelerated then depending upon


which gear is engaged different amount power and
movement is delivered to the car.
END.

Thank you.

You might also like