OOP Flashcards
Distinguish the difference between a class and an insanitation (3)
The class Swimmer is the blueprint of a Swimmer object;
An instantiation is the actual object filled with data;
The class Race stores up to 8 different instantiated objects of the Swimmer class
Define the term mutator method (1)
A method that allows/controls changes to a private (hidden) variable
What is a class?
A class is a blueprint or a template for creating objects (instances).
It defines a data structure that includes attributes (often called fields or properties) and methods (functions or procedures) that operate on those attributes.
Think of a class as a user-defined data type that encapsulates data and behavior.
It serves as a blueprint from which objects can be created.
What is instantiation?
An instance, also known as an object, is a concrete occurrence of a class.
When you instantiate a class, you create a specific object based on that class’s blueprint.
Objects created from the same class have common structure (attributes) and behavior (methods), but they represent distinct entities in the program.
Each instance has its own set of attribute values, independent of other instances.
What is aggregation?
Aggregation is a concept in object-oriented programming and software design where one class represents a whole and another class represents a part of that whole. It is a form of association that represents a “has-a” relationship between objects. In aggregation, the part (or component) can exist independently of the whole, and it can be associated with multiple instances of the whole.
The main idea behind aggregation is to model relationships between objects in a way that allows for flexibility, reusability, and modularity in software design.
The part (or component) can exist independently of the whole.
When the whole is destroyed, the part can continue to exist.
Multiplicity:
A whole can be associated with multiple instances of the part.
The part can be shared among different wholes.
Flexibility:
Aggregation provides flexibility in constructing complex objects by combining simpler ones.
It allows for the creation of complex structures without tightly coupling the classes involved.
Example:
Consider a university system where a Department class aggregates multiple Professor objects. Each professor can be associated with one department, but they can exist independently of the department.
Advantages of aggregation
, y:
Aggregation facilitates code reusability by allowing parts of a system to be reused in different contexts. Since the parts can exist independently of the whole, they can be employed in various scenarios, promoting a more modular and reusable codebase.
Flexibility in Design:
Aggregation provides flexibility in designing and evolving systems. Objects can be combined in different ways to create new configurations without the need for major modifications to existing code.
Easy to Understand Relationships:
Aggregation helps represent relationships between objects in a clear and intuitive manner. The “has-a” relationship is often easier to understand than more complex relationships, contributing to better code readability.
Reduced Coupling:
Aggregation helps reduce coupling between classes, as the whole and part objects remain relatively independent. Changes to one part of the system are less likely to impact other parts, promoting a more loosely coupled architecture.
Natural Representation of Real-world Concepts:
Aggregation often aligns well with real-world concepts, making the code more intuitive and reflective of the problem domain. For example, a university system can naturally represent departments aggregating professors.
Supports Object Composition:
Aggregation supports object composition, allowing developers to compose complex objects from simpler ones. This is particularly useful when building systems with a hierarchical or tree-like structure.
Promotes Code Maintenance:
When changes are needed, the modular nature of aggregation makes it easier to maintain and update specific components without affecting the entire system. This can be especially valuable in large software projects.
Dynamic Relationships:
While there can be challenges with dynamic relationships, aggregation can still support dynamic changes to the structure of objects. Parts can be added or removed dynamically, providing some level of adaptability.
Simplified Testing:
Aggregated components can often be tested independently, simplifying the testing process. This isolation of components makes it easier to identify and fix issues within specific parts of the system.
Disadvantages of aggregation
can cause issues with processing
Complexity:
Aggregation can introduce additional complexity to the design of a system. Managing relationships between whole and part objects, especially when dealing with multiple levels of aggregation, may increase the overall complexity of the code.
Inconsistent Usage:
In some cases, the distinction between aggregation and composition (another form of association) can be subjective. Developers might interpret the relationship differently, leading to inconsistent usage of aggregation, which can impact code clarity and maintainability.
Ownership Ambiguity:
The concept of ownership in aggregation can be ambiguous. It might not always be clear which class is responsible for the lifecycle of the part objects. This ambiguity can lead to issues when it comes to object destruction and resource management.
Dependency:
Aggregation implies a level of dependency between the whole and its parts. Changes in the part class may affect the whole class, and vice versa. This dependency can make the system more rigid and less adaptable to changes.
Navigation Challenges:
Navigating through a complex aggregation hierarchy may become challenging. Traversing from a whole to its parts and vice versa might require intricate code, reducing the overall readability and understandability of the system.
Dynamic Relationships:
Aggregation typically assumes a static relationship between the whole and its parts. If dynamic relationships are required (where parts can be added or removed dynamically), aggregation might not be the most suitable choice.
Performance Overhead:
Depending on the implementation, aggregating objects may introduce some performance overhead, especially if there are frequent queries or manipulations of the aggregated objects.
Potential for Overuse:
In some cases, developers might overuse aggregation when a simpler association or composition would be more appropriate. Overusing aggregation can lead to unnecessary complexity in the code.
Describe one feature of modern programming languages that allows the wide range of students names to be represented correctly (3)
Modern programming languages use Unicode to encode characters.
Which uses 16 bits / has about 64 000 characters.
As opposed to ASCII which uses 8 bits / has 256 characters.
Outline two advantages of the OOP feature ‘inheritance’ (4)
t promotes code reuse;
Because the parent object holds common data and action;
It reduces maintenance overhead;
Because you only have to update the parent;
What is method overriding
Method overriding redefines/replaces a method from the inherited class;
The constructor could only instantiate the finals object;
The method addSwimmers() could fill finals directly;
The method fillFinals() could do nothing;
State the implication of using the term static
The values are the same for all objects
They belong to the class and not the objects
and are only created/declared once
The advantage of using different data types
Having different data types allow different operations to be carried out depending on the type
memory usage can be reduced
OOP basic find loop
- public static/void etc functionName(String variable1, String variable2)
- (
int x = 0
double variable3 = 0.0;
boolean found = false;
while (!found)
(
if ((variable1[x].getZ()) == c)
(variable3 = variable1[x].getX();
found = true;
)
x++;
)
return variable3;
)
Describe two benefits a team would gain from encapsulation
- encapsulation places all attributes and methods that relate to a particular object/entity together
a) example relating to context
b) clearer view or understanding of the problem
c) leading to more efficient programming - encapsulation protects the values of the data stored within the object
from accidental changes in other classes
a) example in given context
b) allows programmers to select any variable names they wish/no restriction on choice of varaible names
encapsulation
refers to the inclusino of both data and actions into a single component
classes contain data and actions that are built in such a way so that their structure is hidden and can only be accessd outside the class specific via methods