OOP Flashcards
Explain the difference between class and objects
Object bundled the attributes and methods that act on the object
Classes is a template used when creating objects, which describes the attributes and methods (interface) that the object will have
Define Encapsulation and state its purpose
Encapsulation is the bundling of data and the methods that acts on the data into an object
Purpose:
1. This is to prevent inadvertent modification of data
2. The interface is separated from its implementation (information hiding from developers)
Advantages of Encapsulation
Feature: prevent inadvertent modification
A: lower likelihood of bugs
Reason: Developers less likely to change an attribute which may cause bugs
Feature: interface separate from implementation
A: More efficient development
R: high modularity; the system can be maintained in two different modules
R: high abstraction; developers don’t need to worry about implementation, and can focus more on how the interfaces interact with each other to achieve their aim
Define Inheritance and state its purpose
Inheritance refers to the child class being able to access parent’s public methods.
Purpose: to encourage code reuse
Advantages of inheritance
Feature: Able to inherit public methods from parents class
A: Reduce the likelihood of bugs
A: Able to complete his task faster and more efficiently
R:
With inheritance, developer only needs to write implementation once and can inherit via super() method and don’t have to write the implementation again.
Thus this reduces duplicate code and reduces reduces code redundancies.
The developer only have to change the implementation at one location instead of many locations,
thus lowering the likelihood of missing a location.
Define polymorphism and state its purpose
Polymorphism refers to the ability of different methods with same name to exhibit different behaviours at runtime.
Purpose: Promotes code generalisation
Advantage of polymorphism
Feature: code generalisation; common interface between objects
A: Enhance efficiency/ Reduces confusion
R:
By having a common interface, developers will not get confused over similar methods name of different objects that carry out the same general function.
A clear and common interface will eliminate the need to remember many similar method names, enabling developers writing the interactions of object to reference the predetermined interface, thus increasing coding efficiency
Things to note when drawing class diagrams? (6)
- Class name has capital first letter and is SINGULAR
- PRIVATE attributes (-)
- PUBLIC getter and setter methods for all attributes (+) (unless really obvious not to)
- Arrow pointing from CHILD class TO PARENT class
- Arrow is hollow
- Read the context and add additional methods where appropriate
What is the difference between Class and object
Class is a template which is used to create objects containing the specified methods and attributes .
Objects bundles the attributes and methods that acts on the object together