OOP questions Flashcards
Advantages of OOP
- Can modify and maintain existing code more easily.
- Reuse of code in other programs through libraries
- Clear modular structure with a defined interface -> can abstract and hide away details.
Advantages of Constants
- They have meaningful identifiers which make code easier to follow and maintain
- Constants only have to be updated in one place if the value changes.
“Inherits” meaning
The child class gains all of the non-private methods of the parent and all of the attributes of the parent.
What is Aggregation Association?
- Weaker association
- The child class cannot exist independently of the parent class
- Real-life example: A person in a car
What is Composition association?
- Stronger association
- The child class cannot exist independently of the parent class.
- Real-life example: A wheel of a car
What is encapsulation?
- Used to hide data and methods, which prevents them from being accessed and changed.
- Distinguishes attributes/methods as public, protected and private.
What is a public method / attribute ?
A method/attribute accessible from outside the class.
What is a private method/attribute?
Private methods/attribute are accessible in their own class
How can you access private attributes?
Using a defined method, e.g. GetSymbol()
What are protected methods / attributes?
Only current class can access methods / attributes.
What programming language doesn’t recognise protected attributes / methods?
Python!
How can you update private attributes?
Using a setter method.
E.g. ChangeSymbolInCell()
What is Overriding?
When a method in a child class overrides a method or attribute in a parent class.
What is Polymorphism?
Allows us to have a function that can take on many forms of its parameters by using overriding.
E.g. the built in function len ()
What is a static attribute?
Static attributes are defined for the class and can be accessed without creating an object instance.
What is an abstract method?
The method isn’t supplied in the base class, which forces the method to be implemented in the subclass (child class)
What is a static method?
A method that doesn’t need to be accessed through object instantation.
What is a virtual method?
A method defined in a superclass that can be overridden by methods in a subclass.
What is the difference between an abstract and virtual method?
Virtual methods have implementation, whereas abstract methods do not.
What is special about every method in Python?
All methods in Python are virtual.
What is an interface?
A class containing abstract methods. It enforces classes to have a fixed set of methods, with the methods being run from the class and not the interface.
How do you indicate inheritance in a UML class diagram?
Have the child class pointing to the parent class using an arrow with its head shaded in.
How do you indicate composition in a UML class diagram?
Using an arrow with its head as a shaded in black diamond.
How do you indicate aggregation in a UML class diagram?
Using an arrow with its head as a white diamond.
How do you indicate that an attribute / method in a UML class diagram is public?
Use a plus sign ( + )
How do you indicate that an attribute / method in a UML class diagram is protected?
Use a hashtag ( # )
How do you indicate that an attribute / method in a UML class diagram is private?
Use a minus sign ( - )
What are the benefits of composition over inheritance?
- Each class can be tested more easily using composition. It isn’t possible to test a child class independently from a parent class using inheritance.
- There can be side effects for child classes if a method in the parent class is changed.
What is the benefit of using encapsulation?
Allows programmers to make future changes to the code more easily, as making private attributes and methods protects other parts of the code from change.
What is the benefit of using interfaces instead of implementation?
Allows programmers to have a framework for all of the classes, as it allows a way to find commonality between unrelated classes that need common methods.