Set 5 Flashcards
What is the advantage of favouring composition over inheritance?
It makes it easier to test each class using unit testing
What is the similarity between composition and aggregation?
Both “has a” relationships (ownership)
What is the difference between composition and aggregation?
- In composition, if the containing object is destroyed so are the objects it contains
- This is not the case with aggregation
(composition = strong ownership, aggregation = weak ownership)
Give five advantages of using subroutines:
- Code re-use
- Allows for modularisation of the program
- It makes it easier to identify bugs
- It makes it easier to test individual tasks NE easier to test
- It makes it easier for other programmers to interpret and understand your code
What is an object reference variable?
- A fixed length memory address that points to a memory location (in the heap)
- It is used to refer to an object
What is the purpose of a class?
To define the method and property fields that capture the common behaviours and characteristics of objects
How is composition represented in UML?
Black diamond
How is aggregation represented in UML?
White diamond
What is encapsulation?
- The process of combining properties and methods together into an object
- and being able to restrict access to an object’s state / behaviour
What is a constructor?
A method that is called when an object is first created
What is the purpose of inheritance?
To create a hierarchy of specialisation for classes
What is (subtype) polymorphism? What is its purpose?
- When a method in a parent class also exists in a derived class
- The method has the same name and parameter list, but a different implementation
- It allows objects up and down the inheritance hierarchy chain to respond differently to the use of a common interface
Give two advantages of “programming to interfaces, not implementation”:
- Allows you to test each part of your program separately
- Allows you to modify your code easily in the future
Give two advantages of local variables over global variables:
- You can give local variables the same name in different subroutines because they are only recognised by the subroutine they are declared in
- Local variables are deleted as soon as the subroutine is over and release the memory space which it occupies
What is a constant?
A data item that, once declared, retains the same value for the entire duration of the program run
Give two advantages of using constants over local variables:
- Makes your code more readable, by clearly indicating that a value should not be changed throughout the program
- Enhances code safety by preventing accidental modifications to values that shouldn’t change
What does the access modifier protected
mean? Give its UML symbol.
- It can be accessed by any subclasses
- and from within the class itself
#