[go: up one dir, main page]

0% found this document useful (0 votes)
35 views31 pages

Lab9 OOP Slides

Uploaded by

melt.bonier.0z
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)
35 views31 pages

Lab9 OOP Slides

Uploaded by

melt.bonier.0z
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/ 31

OBJECT ORIENTED PROGRAMMING

(OOP)
OBJECT ?
Brand Color
Price

Country of Origin Power (hp)

Steer Horn
Shift Gear
CAR OBJECT

Car

• Brand
• Power
• Color
• Country of Origin
• Price
• Steer
• Shift Gear
• Horn
WHAT IS AN OBJECT ?
• Real-world objects share two characteristics:
• They all have state and behavior.
• Ex : Cars have state (color, price) and behaviour (steering, accelerating)
Dogs have state (name, breed) and behaviour (barking, wagging tail)

• Software objects are conceptually similar to real-world objects


• They consist of state (data attribute) and related behaviour (method) as well

• An object stores its state in data attributes and exposes its behavior through
methods
WHAT IS A CLASS ?
• In the real world, there are many individual objects all of the same kind.
• Each automobile was built from the same set of blueprints and therefore contains the same
components.
• In object-oriented terms, we say that your automobile is an instance of the class of
objects known as automobiles.

• A class is the blueprint from which individual objects are created.


WHAT IS A CLASS ?
• In the real world, there are many individual objects all of the same kind.
• Each automobile was built from the same set of blueprints and therefore contains the same
components.
• In object-oriented terms, we say that your automobile is an instance of the class of
objects known as automobiles.

• A class is the blueprint from which individual objects are created.


name class parent (optional)

class definiton
ATTRIBUTES
• Data and procedure belonging to that class.

• data attributes:
• Data that makes up the class.
• A car has a brand name, color, price etc.

• methods:
• Functions that only work with the class
• A car can sterr, accelerate etc.
How we access an attribute
OUTPUT
USING __init__
Special __init__ method initialize some data attributes

Special method to create an instance


parameter to refer an instance of the class

data attribute
USING __init__
Special method to create an instance
parameter to refer an instance of the class

data attribute

Initialization
OUTPUT
METHODS
PYTHON OBJECTS
• EVERYTHING IN PYTHON IS AN OBJECT !

• Strings -> myString.capitalize()


• Lists -> myList.append(9)
• Dictionaries -> myDictionary.items()
.
.
.
ADVANTAGES OF OOP
• Encapsulation
• Hiding internal state and requiring all interaction to be performed through an
object's methods
• Modularity
• Once created, an object can be easily passed around inside the system.
• Information-hiding
• By interacting only with an object's methods, the details of its internal
implementation remain hidden from the outside world.
• Code re-use
• If an object already exists (perhaps written by another software developer), you can
use that object in your program.
• Pluggability and debugging ease
Defining Your Own Print Method __str__

OUTPUT
Defining Your Own Print Method __str__

OUTPUT
Special operators
• +, -, ==, <, >, len(), print, and many others
https://docs.python.org/3/reference/datamodel.html#basic-customization
• like print, can override these to work with your class
• define them with double underscores before/after
WHY DO WE USE OBJECTS ?
Name
Surname
E-mail address
Student ID
Grade
Student1 Student2 Student3

• John • Anna • David


• Smith • Jones • Williams
• john.smith@boun.edu.tr • anna.jones@boun.edu.tr • david.williams@boun.edu.tr
• 2023123456 • 2023234567 • 2023345678
• 75 • 87 • 56
https://registration.boun.edu.tr
Academic Records
https://mountandblade.fandom.com/wiki/Character_creation
Binary Tree Linked List
References
• MIT OPENCOURSEWARE- Introduction to Computer Science and
Programming In Python, Fall 2016
• Oracle Object-Oriented Programming Concepts :
• https://docs.oracle.com/javase/tutorial/java/concepts/index.html

You might also like