Object Oriented Programming Flashcards
superclass <– subclass
How are objects and classes related?
A class (among other definitions) is a set of objects. An object is a being belonging to a class.
An object is an incarnation of the requirements, traits, and qualities assigned to a specific class.
What is inheritance?
Any object bound to a specific level of a class hierarchy inherits all the traits (as well as the requirements and qualities) defined inside any of the superclasses.
Inheritance is a common practice (in object programming) of passing attributes and methods from the superclass (defined and existing) to a newly created class, called the subclass.
What are the three attributes of an object?
an object has a name that uniquely identifies it within its home namespace (although there may be some anonymous objects, too)
an object has a set of individual properties which make it original, unique, or outstanding (although it’s possible that some objects may have no properties at all)
an object has a set of abilities to perform specific activities, able to change the object itself, or some of the other objects.
What is object programming?
the art of defining and expanding classes
What is instantiation?
The act of creating an object of the selected class is also called an instantiation (as the object becomes an instance of the class).
What is the alternative name for a stack?
LIFO
last in - first out
What is a stack?
A stack is a structure developed to store data in a very specific way.
A stack is an object with two elementary operations, conventionally named push (when a new element is put on the top) and pop (when an existing element is taken away from the top).
What is encapsulation?
the encapsulated values can be neither accessed nor modified if you want to use them exclusively;
What does the constructor do?
Construct a new object within a class
The constructor should know everything about the object’s structure, and must perform all the needed initializations.
What is the constructors name and parameter?
__init__(self)
It is invoked implicitly and automatically with he class
How do you access a property of the contructor?
self.name_property
How do you create a private class componenet?
two underscores before name __
it can only be accessed within the class and you cannot see it from the outside world (encapsulation)
All method within a class need….
The self parameter
It allows the method to access entities (properties and activities/methods) carried out by the actual object.
How do you define a subclass of a stack?
e.g. class Stack:
class SubStack(Stack):
Python forces you to explicitly invoke a superclass’s instructor. True or False
def __init__(self):
superclassname.__init__(self)
example of superclass and subclass
class Stack:
def __init__(self):
self.__stk = []
def push(self, val): self.\_\_stk.append(val) def pop(self): val = self.\_\_stk[-1] del self.\_\_stk[-1] return val
class CountingStack(Stack):
def __init__(self):
Stack.__init__(self)
self.__counter = 0
def get_counter(self): return self.\_\_counter def pop(self): self.\_\_counter += 1 return Stack.pop(self)
Describe an instance variable
If the value of a variable varies from object to object, then such variables are called instance variables. For every object, a separate copy of the instance variable will be created.
An instance variable is a property whose existence depends on the creation of an object. Every object can have a different set of instance variables.
Moreover, they can be freely added to and removed from objects during their lifetime. All object instance variables are stored inside a dedicated dictionary named __dict__, contained in every object separately.
What is a class variable?
A class variable is a property which exists in just one copy and is stored outside any object.
Two important conclusions:
class variables aren’t shown in an object’s __dict__ (this is natural as class variables aren’t parts of an object) but you can always try to look into the variable of the same name, but at the class level - we’ll show you this very soon;
a class variable always presents the same value in all class instances (objects)
- if the class is called three time and there is a counter the counter will be three for all printouts of the objects
What is mangling?
When you try and call a private variables you need to put _classname__varaible
class ExampleClass:
__counter = 0
def __init__(self, val = 1):
self.__first = val
ExampleClass.__counter += 1
example_object_1 = ExampleClass()
print(example_object_1.__dict__, example_object_1._ExampleClass__counter)
- An instance variable can be private when its name starts with __, but don’t forget that such a property is still accessible from outside the class using a mangled name constructed as _ClassName__PrivatePropertyName.