Fundamentals of Programming - Notes Flashcards
How are stack frames used by computers
To store return address, parameters and local variables for each subroutine call that occurs during the execution of a program
How does exception handling work
-When an error occurs, the computer has to handle that error to avoid crashing
-It pauses execution and saving the current state of the program
-Runs the catch block of code
-Once error has been handled, the program restores its previous state before resuming execution
When does polymorphism occur
When a method in an object is processed differently depending on the class
Two types of association:
Aggregration and Composition
Relationship of Aggregration
Weak as an associated object will still exist when an object is deleted
Relationship of Composition
Strong as when an object is deleted, the associated object is also deleted
Whats an abstract method
Declared in the abstract class and do not have an implementation, they serve as a blueprint for methods that must be implemented by a subclass. Ensuring consistency between different subclasses.
Whats a virtual method
Methods in a base class that can be overriden by derived classes. They faciliate polymorphism, enabling method behaviour to vary based on the objects that invokes the method.
Encapsulate what varies definition
When designing a program, any requirements which are likely to change in the future should be encapsulated in a class so that any changes can be easily made when required
Favour composition over inheritance
Wherever possible, composition should be used over inheritance as composition is seen to have a more flexible relationship over objects
Program to interfaces, not implementation
This design principle allows unrelated classes to make use of similar methods. An interface is defined as a collection of abstract procedures that can be implemented by unrelated classes
How is aggregration and composition drawn
Aggregration : weaker so unfilled diamond arrow head
Composition : stronger so filled diamond arrow head
Benefit of principle ‘Favour composition over inheritance’
Easier to test each class using unit testing
There can be unintended side effects for derived classes if a base class is altered.