OOP Flashcards
Class
Defines a type of object using OOP.
each object is an instance of the class
Attribute
a value within an OOP class
OOP Method
procedures or functions written within an OOP class
OOP Object
an independent entity
OOP defines the attributes of an object
OOP defines the methods that can be applied to the object
many instances of the object can be declared
Constructor in OOP
takes place immediatelly after defining the class .
-sets attributes
- gives them starting values
- provides parameters
write a constructor in python
(with two attributes called att1 and att2, set to the parameters p1 and p2)
def __init__(self, p1, p2):
self.att1=p1
self.att2=p2
advantages of OOP
easier to read code
less work in long run
better control over the process
fewer erros
good for team work
disadvantages of OOP
needs careful planning
more work at the start
objects must be similar
class Car():
def__init__(self, p1, p2):
self.colour = p1
self.make = p2
self.speed = 0
create an instance of the car class called mycar that is a pink miata
mycar = Car(“Pink”,”Miata”)
imagine this method is written within an OOP class:
def gofaster(self):
self.speed = self.speed + 10
in the main program, write a line of code to use this method on mycar
mycar.gofaster()
rewrite this constructor in psuedocode:
class Car():
def__init__(self, p1, p2):
self.colour = p1
self.make = p2
self.speed = 0
class Car
procedure new(p1, p2)
colour = p1
make = p2
speed = 0
end procedure
end class
inheritance
one class can “inherit” all the features of another class.
show the first line of a class definition of a class Y that inherits class X
Class Y inherits X
what do we call the class that inherits / the class it inherits from
super class and sub class
or
parent class and child class
encapsulation
using private methods and attribites in OOP
- they cannot be altered directly, only accessed via “get” and “set” methods.