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()