Object Oriented Programming Flashcards
(38 cards)
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)