OOP terms Flashcards
Programming paradigm
A style of computation and programming (chosen according to the type of problem)
Hierarchy chart
A top-down, visual representation of a structured program that breaks down the overall problem into smaller sub-tasks which can be individually coded. Each procedure is represented with a rectangle, and lines show the relationships between these
Structured approach
A approach to programming that uses 4 structures to develop a program: assignment, selection, sequence and iteration
Structured programs are designed top-down and so break important elements of a problem into smaller tasks which can be solved in a block of code such as a subroutine. This makes maintenance of larger programs easier, and developing, debugging and testing easier to navigate
Object-orientated programming (OOP) (programming paradigm)
A programming paradigm where the system is viewed as a system of objects, each with their own attributes and methods that interact with each other. All processing is done by the objects
Class diagrams
Visual representations of the relationships between classes and subclasses
Class
A template/blueprint defining the attributes and methods that can be used to create objects. Classes contain the structure of the code and specify which attributes and methods objects of their type will have (i.e. objects that have been created from that class). Classes cannot themselves be executed
Class attribute
An attribute in a class which is available to all classes without inheritance
To use a class attribute in its class or a child class, the class name should be put before it, e.g. Jungle.feedback (where Jungle is the name of the class and feedback is the name of the class attribute)
When defining a class attribute in the constructor, you don’t use “self”, i.e. feedback = 5, not self.feedback = 5
Overriding
A process that occurs in polymorphism. When a method has a different implementation from the method with the same name in the parent class. A method is overridden when it has a different implementation to the method with the same name in the inherited (child) class
What is the purpose of a super() call?
It allows a method in a parent class to be used/calls a method in the parent class, even if it is overridden
Public attributes/methods
No underscore, e.g. self.name
Can be accessed from anywhere (incl. outside the class)
Protected attributes/methods
One underscore, e.g. self._name
Can be accessed from within the class and by child classes
Private attributes/methods
Two underscores, e.g. self.__name
Can only be accessed from within an object/class. Public methods can also be used to access/modify a class’s private attributes
Polymorphism
The idea that what a subclass inherits can be replaced with the subclass’s own version. A subclass can therefore override a method which it inherits from its parent/base class
In a non parent-and-child-class context, this can simply refer to two classes having a method with the same name, that do different things (but it is usually in the context of the top way)
Objects of different classes can use different methods to perform the same action
Inheritance
A child class is able to “inherit”, i.e. access, attributes/methods from its parent class, e.g. by using the super() call. This allows a class to share the properties of another class, while still having its own ones too. Inheritance is described as an “is a” relationship, i.e. a “Laptop” class would inherit from a “Computer” class because a laptop is a computer and so would share its properties, but may also have some that are specific to just itself as well
Inheritance makes code less repetitive, more organised, and avoids duplication
The “main” function
A function which uses standard-boiler-plate to tell the python interpreter that this function is the starting point of the code
The main function will contain all of the object instantiations, e.g.
def main():
r = Jungle()
r.sayname()
Standard-boiler-plate is then used to indicate this function should be run first:
if __name__ == “__main__”:
main()
Constructor
The __init__ method which defines/initialises methods and attributes for the class
The constructor of the child class is also where inheritance of attributes/methods/constructors of the parent class is done
The constructor method is executed automatically as soon as the object is created for a class (i.e. without being called)
What are the benefits of OOP?
- It increases the re-usability of code
- Code becomes more manageable
- The clearer structure makes it easier for testing and developing
- The paradigm makes it easy for projects to be split among many programmers (e.g. they can all take on one class each)
- The space-efficacy of code is good (as code can be reused in the program and used in other programs)
Method
A function in a class. They can only be used once an object of the class is created. Names of functions are written with a lowercase letter
Object
An instance of a class, which can be executed. They use the structure of a class to perform various tasks
Instantiation
The creation of an object from a class
Encapsulation
The process of combining methods and procedures to form an object (a way of maintaining data integrity)
An object is said to “encapsulate” its contents by forming a single entity encompassing specific methods/attributes
The ability to protect attributes inside a class from being accessed directly - through the use of private or protected modifiers. Instead you are forced to use public methods to interface with the attributes
Procedural programming (programming paradigm)
Programs are formed from sequences of instructions executed in the order they appear. Functions and procedures make up the code and can be called from anywhere in the program or recursively
Data is stored by constants and variables. These have a “global scope” if they can be accessed from anywhere in the program, and a “local scope” if they can only be accessed from the subroutine they are in
Procedural programming uses the structured approach
Association
When two objects/classes have a “has a” relationship. In this relationship, one of the objects will be the container object, which will “have” the other object as one of its associated properties, e.g. if there is a Car class and a Driver class, the Car class is the container object, and the Driver class the associated object. They have a “has a” relationship as a car “has a” driver
There are two types of association - aggregation and composition
Aggregation
This is a weak form of association. When an object is associated with another through aggregation, if the containing object is destroyed, it will still exist, e.g. a car and a driver