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.
get and set methods
if an attribute is private we cant show or change it in the main program, so get and set methods are used instead
write a get method for the attribute colour in pseudocode
public procedure getcolour()
return colour
end procedure
write a set method for the attribute colour in psuedocode, using p1 as the parameter
public procedure setcolour(p1)
colour = p1
end procedure
What is inheritance in OOP
A sub class can inherit attributes and methods from an existing super - class. The subclass is then defined by making alterations or additions to the super- class.
(psuedocode) here is a superclass.
class Employee
public procedure new(surname, jobtitle):
surname = surname
jobtitle = jobtitle
end procedure
endclass
now write a subclass for manager with their name and title, explaining which line inherits what.
-class manager inherits Employee
- public procedure new (thisname, thistitle)
- super.new( thisname, thistitle)
- end procedure
-endclass
the first line inherits all the methods of the superclass
the third line inherits all attributes from the super class
here is the pseudocode for a sub- class .
class manager inherits Employee
public procedure new (thisname, thistitle)
super.new( thisname, thistitle)
end procedure
endclass
add a line to this constructor, that will add a new attribute called “manages” and set the variable to 0 .
class manager inherits Employee
public procedure new (thisname, thistitle)
super.new( thisname, thistitle)
manages = 0
end procedure
endclass
(psuedocode) here is a class defenition. Now write it in python
class Employee
public procedure new(surname, jobtitle):
surname = surname
jobtitle = jobtitle
end procedure
endclass
Class Employee():
def __init__(self, surname, jobtitle)
self.surname = surname
self.jobtitle = jobtitile
here is a superclass written in python.
Class Employee():
def __init__(self, surname, jobtitle)
self.surname = surname
self.jobtitle = jobtitile
now write a subclass for manager with their name and title
class Manager (Employee):
def __init__(self, thisname, thistitle)
super(). __init__ (thisname, thistitle)
end procedure
endclass
in python, write the opening line for a class constructor called car.
then, write the opening line for a class constructor called car which inherits the class vehicle
class Car():
class Car(Vehicle):
in psuedocode, write the opening line for a class constructor called car.
then, write the opening line for a class constructor called car which inherits the class vehicle
class Car
class Car inherits Vehicle
list 4 advantages of inheritance
- reduces the amount of work
- fewer new things to learn
- reduces risk of errors
- tried and tested methods
How could inheritance be used for logins
The admin login class could inherit attributes of the normal login class, with extra security checks.
This reduces the amount of work the programmer has to do and reduces the risk of errors. As methods are reused
when using inheritance, what are the 3 different options for a constructor ( the part of the definition which defines and sets attributes)
No constructor - use attributes of superclass
Inherit constructor - Add new attributes to build on superclass
Make a new constructor
what is the line to inherit a constructor in python. Imagine that the attributes being inherited are colour and size
super(). __init__ (colour,size)
when attributes use encapsulation, what does this mean
they have been made private
they cannot be shown in the main program
they cannot be changed in the main program
is encapsulation a feature in python
no
what , other tha attributes, can have encapsulation used on them
methods
how to tell if encapsulation has been used in psuedocode
write “public” or “private” before it.
eg.
public procedure new (…)
in pseudocode, if i wanted to make one of the attributes in a class definition private, how would i do it?
write an example of a class called shape with the colour being private
the same way you call global variables at the start of a module
class Shape
private colour
…. (rest of constructor)
in the main program, i want to set the boolean attribute “verified” on user01 to true. However, it is encapsulated and needs to be accessed using a set method.
Write the single line of code
instead of using
user01.verified = True
use
user01.setverified(True)
what does polymorphism mean
the same command has different effects with different data types or classes
eg. py + symbol is different for numerical and string variables
how does polymorphism apply to OOP
A method defined for one class of object cant be used with another
A method in a different class might have the same name but do a different thing
imagine a geometry program with classes called “square”, “triangle” and “circle”.
All of them have a method “calculate_area”
explain why this is an example of polymorphism
the method will work in different ways for the different classes - it will be defined with the correct formula in each class.