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

Object Oriented Programming23

The document discusses object-oriented modeling concepts such as classes, objects, attributes, methods, encapsulation, abstraction, inheritance, polymorphism, and interfaces. It provides examples of how classes define common traits of objects through attributes like color and methods like start(), and how objects are instances of classes that can inherit and override methods. Interfaces and abstract classes are also introduced as ways to define expected behaviors without specifying implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Object Oriented Programming23

The document discusses object-oriented modeling concepts such as classes, objects, attributes, methods, encapsulation, abstraction, inheritance, polymorphism, and interfaces. It provides examples of how classes define common traits of objects through attributes like color and methods like start(), and how objects are instances of classes that can inherit and override methods. Interfaces and abstract classes are also introduced as ways to define expected behaviors without specifying implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Object-orient modeling

System Requirements and Design


T-233-SRAD

Berglind Kara | February 3th, 2023


Object-oriented modeling
• Object-orient programming (ísl. hlutbundin forritun) is a programming paradigm
based on the idea of objects, that have attributes (ísl. eigindi) and methods (ísl.
aðferðir).
• Objects have attributes that define the instance of the object.

• Objects have methods that define how they interact.

• Every object is an instance (ísl. tilvik) of a class.

• UML is the standard tool to describe these systems.

• Object oriented modeling’s abstraction facilitates the documentation of reusable


solution patterns, so called design patterns.
Some concepts
Modularity (ísl. einingastig) Coupling (ísl. samtenging)
• Break system down to modular parts • Dependency of modules of the
or distinct components. system. The tighter the coupling, the
harder it becomes modifying it or it´s
• Helps make the system easier to
dependent environment.
understand, to test, and reason
about.
• Offers reusability.

• Modules offer an interface, or a


contract that defines how they are
used.
What is an class?
• A class (í. klasi) is a groupping of similar traits of objects.
• Traits: attributes, methods
• Objects are instances (í. tilvik) of classes.
• A house class describes
what houses are and do.
• An instance of a house
class defines what that
particular house looks like.
A constructor (ísl. smiður)

class House:
pass i n i t ( s e l f , c o l o r ) :
def
s el f . c ol or = color Class

firstHouse = House()
firstHouse = House("Yellow")
secondHouse = House()
secondHouse = House("White")
thirdHouse = House()
thirdHouse = House("Pink")
Instance
f i r s t H o u s e . c o l o r = "Yellow"
print(firstHouse.color)
secondHouse.color = "White"
print(secondHouse.color)
thirdHouse.color
p “ Pl oi nr )k "
r i n t ( t h i r d H o u s e .=c o
print(firstHouse.color)
p r i n t ( secondHouse.color)
print(thirdHouse.color)
Car
color Classes vs. instances
model
makeYear
fuelType • Each object is an instance of a class.
start() • A green car is an instance of the car class,
stop() and so is an yellow car.
accelerate()
• The green car might be of the same model as
the yellow, or a different one. But the color is
different at least.

• The methods are the same. How the object


can interact.
Attributes and methods
Attributes are values that describe the Methods are functions that describe what
object. the object can do (and how it does it).
Car Car
color color
model model
makeYear makeYear
fuelType fuelType
start() start()
stop() stop()
accelerate() accelerate()
class Car:
def s t a r t ( s e l f ) :
class Car: pass
def i n i t ( s e l f , c o l o r, model, makeYear, f u e lTyp e ): def s t o p ( s e l f ) :
s e l f . c o lo r = color pass
self .model = model def a c c e l e r a t e ( s e l f ) :
self.makeYear = makeYear pass
s e l f . f u e l Ty p e = fuelType
The four pillars of OOP - encapsulation
Encapsulation (ísl. hjúpun)
• Encapsulation bundles data and
methods that work on data within
one unit.
• Allows the implementation to hide
details of it’s inner working.
• Programmer specifies what
attributes and objects are needed to
interact with the class.
The four pillars of OOP - abstraction
Abstraction (ísl. útdráttur) • Part or all of the class attributes and
methods can be made non-
• Abstraction helps to hide
accessible to other parts of the
unnecessary details from the user.
program.
• An extension of encapsulation. • Public method and attributes can be
used and accessed.
• You don’t know how the car engine • Private methods and attributes cannot
works, just that you know how to use be accessed and used except only the
Instance itself.
it in your car. What happens beneath • Protected methods and attributes have
the hood is hidden from the user. limitations regarding who and what can
access them, but wider than private.
The four pillars of OOP - inheritance
Inheritance (í. erfðir) Rectangle
lenght
• Finding the common traits in related
width
object types.
calculateArea()
• Classes (object types) are often very
similar, share common attributes Box inherits from rectangle
and methods. But some might differ
slightly.
• Take a square and a rectangle. All
Box
height
squares are rectangles, but not all
rectangles are squares. calculateArea()
calculateVolume()
The four pillars of OOP - inheritance Box inherits from rectangle
Rectangle Box
Inheritance (í. erfðir) lenght height
width
• Finding the common traits in related calculateArea()
object types. calculateArea() calculateVolume()

• Classes (object types) are often very class Rectangle:


def i n i t ( s e l f , length, width):
similar, share common attributes s e l f . l e n g t h = length
and methods. But some might differ s e l f . w i d t h = width
slightly.
class Box(Rectangle):
• Take a square and a rectangle. All def i n i t ( s e l f , length, width, height):
squares are rectangles, but not all super(Box, s e l f ) . i n i t ( l e n g t h , width)
s e l f . h e i g h t = height
rectangles are squares.
The four pillars of OOP - polymorphism
Polymorphism (í. fjölvirkni) • Derived classes can also add new
attributes and methods as needed.
• Common behavior is defined in a
base class, and overridden in • The Student class, that is derived
derived classes. from the person class, could have
the enrollToClass method for
• The box in our example from before
example.
now has to implement another way
of calculating the surface area. • Another example:

• So our box class overrides the


Writing instrument
calculateArea method.
Interfaces and abstract classes
(ísl. skil og hugrænir klasar)
Interfaces allow us to create a Abstract classes allow us to og one
specification of what a set of classes step further and define parts of how the
should be able to do. class is supposed to be implemented.
We don’t specify how it’s supposed to
be implemented, but rather just what
Let´s take a look at how this could be
needs to be implemented.
done in Python.
We can use these interfaces to validate
that derived classes follow the
expected behavior.
Interfaces and abstract classes, cont.
(ísl. skil og hugrænir klasar)
from abc import ABC, abstractclassmethod from abc import ABC, abstractclassmethod

class CarInterface(ABC): class CarInterface(ABC):


@abstractclassmethod @abstractclassmethod
def s t a r t C a r ( s e l f ) : def s t a r t C a r ( s e l f ) :
pass pass

@abstractclassmethod @abstractclassmethod
def s t o p C a r ( s e l f ) : def s t o p C a r ( s e l f ) :
pass pass

car = C a r I n t e r f a c e ( ) class Vo l vo ( Ca r I n t e r f a c e ) :
c a r. s t a r t C a r ( ) def s t a r t C a r ( s e l f ) :
print("Brumm brumm")
return

car = Vo l vo ( )
c a r. s t a r t C a r ( )
from abc import ABC, abstractclassmethod Interfaces and abstract classes,
class CarInterface(ABC): cont. (ísl. skil og hugrænir klasar)
@abstractclassmethod
def s t a r t C a r ( s e l f ) :
pass • Each class that implements an interface,
@abstractclassmethod must implement all of it‘s methods.
def s t o p C a r ( s e l f ) :
pass • If a method isn‘t implemented a
class Vo l vo ( Ca r I n t e r f a c e ) : TypeError will be thrown.
def s t a r t C a r ( s e l f ) :
• TypeError: Can't i n s t a n t i a t e abs trac t
print("Brumm brumm") class Volvo wi th abs trac t methods
return stopCar
def s t o p C a r ( s e l f ) : • Python offers the abc (Abstract Base
return
Class) module for using interfaces and
car = Vo l vo ( ) abstract base classes.
c a r. s t a r t C a r ( )
OOP and UML
• UML has several types of diagrams that are used to describe classes and
operations involving classes:
• Class diagrams (ísl. klasarit)– one of the most common diagrams used.

• Object diagrams – shows a snapshot of the detailed state of a system in a point


of time. Similar to a class diagram, but with values instead of abstractions.
• Sequence diagrams (ísl. runurit) – shows interactions between objects.
Háskólinn í Reykjavík | Menntavegur 1 | 101 Reykjavík | Sími: 599 6200 | www.hr.is

You might also like