Object-orientated programming Flashcards
Class
A class is a template for an object and defines the attributes and behaviours of the objects derived from that class
- Attributes = Data associated with the class
- Methods / behaviours are what the class can do and what can be done to the class
One major benefit of OOP is that classes can be reused to create lots of objects
Instantiation = The creation of objects from a class template
Inheritance
Inheritance allows a class to inherit attributes, properties and behaviours from another class
The class that passes down this data is called the parent class, the superclass, or the base class, while the classes that inherit this information is called the subclass or child class
The subclass can reuse and extend the attributes and behaviours of the superclass - this promotes code reuse and allows the subclass to have its own unique features
Inheritance supports the ‘is a’ relationship, where an object of the child class is treated as an object of the parent class
eg: is a Cat an Animal? where the superclass is Animal and the subclass is Cat
Encapsulation
Encapsulation is where data fields are combined with the functions and procedures that operate on those data fields
This provides the ability to hide the internal data of an object and its methods and only allows the user to communicate through the public interface
Information hiding is where the implementation details of the class are hidden; only the essential details of the class are exposed
Benefits to encapsulation include keeping functionality in one place and protecting data from being modified by external code
Polymorphism
Polymorphism is where a method is shared up and down the class hierarchy by a base class and its subclasses - each subclass redefines the method in its own way - it builds on inheritance
This helps programmers reuse defined methods once they are written - promotes code reuse
Overriding is when the subclass implements the base class’ method in its own way
Aggregation and Composition
Aggregation is a relationship between objects that resembles a ‘has a’ relationship where one class, known as the whole, references another class, known as the part, but where the part can exist without the whole and is shareable with other wholes
eg. Library has books but books can exist without the library
Composition is a stronger form of aggregation, where the part cannot exist without the whole and can’t be shared with other wholes
eg. Car needs Engine to drive around and wont work without one. If the Car is destroyed, so is the Engine and the Engine can’t exist without the Car
OOP advantages
- Produces reusable components; classes and objects can be used elsewhere and methods and attributes can be passed down and shared up the class hierarchy
- Encapsulation hides the internal state of an object, which, along with information hiding, allows for better data protection
- Easier to understand with well-defined objects and abstraction
- Easier to maintain, update and debug code with OOP modularity