Object Oriented Programming Flashcards
Object-Oriented Programming (OOP)
Paradigm where the program is composed of interacting objects, each responsible for its own data and operations
Class
Template for an object that defines its attributes and methods
Object
A specific instance of a class
Attribute
Data associated with the class
Method
A functionality of a class
Object state
The actual data values of a particular object’s attributes
Instantiation
Creation of a new object
How to instantiate object
By calling class’s constructor method → Object returned can be assigned to identifier variable
Types of methods
- Getter methods
- Setter methods
Getter methods
Written as functions and return the object’s state
Setter methods
Written as procedures and used to change object’s state
Encapsulation
Wrapping all of an object’s related data and methods under one entity → Objects can’t affect the way other objects function
Benefits of encapsulation
- Promotes modularity → Self-contained units of functionality that can be re-used
- Reduces complexity (easier to understand how object fits into system if it’s self-contained)
- Can change implementation of entity without impacting other parts of program
- Improve security (information hiding)
- Easier to maintain and test code (easier to interface with individual components)
Inheritance
- Principle that allows us to create a new class that is a modified version of another class
- Subclass gains all the attributes and methods of the superclass
- Subclasses can be overidden and properties can be polymorphised
- New data and behaviour can also be added to subclasses
When to used inheritance
- Is [subclass] a [superclass]?
- E.g. is a Fox an Animal?
Benefits of inheritance
- Reuse of code and avoids duplication → Saves time and effort when writing & testing
- Hierarchy of classes → Better organisation of code
- Specialised classes designed for specific tasks that inherit from a general class (e.g. Fox class inherits from Animal)
- Polymorphism and overriding → ↑ Flexible and powerful code
Overriding
When a subclass replaces and inherited method
Polymorphism
When a subclass has a different implementation of an inherited method to the superclass
Inheritance diagram
Child classes point to parent class with unfilled arrow
Object association
When an object either contains or has reference to another related object (‘has a’ relationship
Types of association
- One-to-one
- One-to-many
- Many-to-many
- Aggregation
- Composition
Aggregation
- Weaker form of association
- When an object (whole) is composed of or contains multiple other objects (parts)
- Lifecycle of part not dependent on whole
- E.g. Team and Player have an aggregative association
Composition
- Strong form of association (and aggregation)
- Lifcycle of part depends on whole
- E.g. Hotel and Room have a compositive association (rooms can’t exist if hotel is destroyed)
Reasons to favour composition over inheritance
- ↑ Flexibility (stuck with inherited attributes & methods vs Picking and choosing required properties)
- ↑ Explicit & easier to understand (properties of composite class clearer)
- ↑ Robust (changing superclass may change unintentionally change subclass)
- ↑ Efficient (if subclass doesn’t use all inherited properties from superclass → Wasteful)
Information hiding
- Practice of keeping internal details of class hidden from rest of code → Prevents exposing inner workings of class
- Implemented using access modifiers and encapsulation
- Object’s state manipulated using getter and setter methods
Access modifiers
- Public (accessible by any object from any class in program)
- Protected (accessible by other objects of same class or subclasses)
- Private (only accessible to that object)
What access modifiers to use?
- Attributes should be private (prevents non-interface access)
- Most methods should be public (allows access to interface)
Programming to interface
- Designing code to depend on interfaces rather than concrete implementations → Multiple classes can have different implementations of same method
- Part of encapsulation
Interface
A collection of abstract methods that a group of unrelated classes must implement (e.g. multiple appliances must be able to switch on and off, so this can be implemented as an interface)
Coding interfaces
Public interface Switches Procedure SwitchOn Procedure SwitchOff Procedure SetTimer(time) Function GetTimer etc End Class Microwave implements Switches
Interfaces in Python
Implemented as abstract classes
from abc import ABC, abstractmethod class TwoDimensionalShape(ABC): @abstractmethod def get_area(self): pass
Encapsulate what varies
- Encapsulating aspects of program that are likely to change into separate classes or modules → ↑ Flexible and maintainable code
- Separates stable parts of code from more volatile ones → Easier to understand and modify code without having to modify code that doesn’t need to change
- Can be implemented using interface
Advantages of OOP
- Methodology forces designers to go through an extensive planning phase → better designs with fewer weaknesses
- Encapsulation - source code for an object can be written, tested and maintained independently
- Once created, knowledge of an object’s specific implementation isn’t necessary for a programmer to use it
- New objects can be easily created with small differences from existing ones
- Reusability - objects that are already defined coded and tested may be used in many different programs → OOP provides good framework for code libraries
- Software maintenance - an object-oriented program is easier to maintain due to rigidly enforced modular structure
UML (Unified Modelling Language) Diagrams
Models the classes in a system and the relationships between them
Class Boxes
- Name of class
- Attributes : Type
- Methods (: Return type)
Relationship lines
- Inheritance = Pointy arrow
- Aggregation = Clear diamond arrow
- Composition = Filled diamond arrow
Visibility markers
- Public = +
- Private = -
- Protected = #
Formal class definition
MediaFile = Class: Public: Procedure PlayFile Function GetTitle : String Function GetDuration : Integer Private: Title : String Duration : Integer MusicFile = Subclass(MediaFile): Public: Function GetArtist : String Function GetSampleRate : Integer Funcion GetBitDepth : Integer Private: Artist : String SampleRate : Integer BitDepth : Integer