Object Oriented Programming Principles Flashcards

1
Q

What are the 4 Basics of OOP?

A

Encapsulation
Abstraction
Inheritance
Polymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Encapsulation?

A

Controlling access to the state.

Encapsulation is when an object, containing properties and methods, encapsulates the how the properties/data/state is accessed via an interface - specific methods that have permission to access and or mutate properties.

EG1: An instance of a bank account class would hold within it the data of the current balance for that account. However it would be very dangerous if the balance could be accessed directly, so an interface provided might specify ‘withdraw’ & ‘deposit’ methods for specific processes.

EG2: Getters & Setters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Abstraction?

A

Hiding unnecessary details from the outside world.

Abstraction is when an object hides methods and properties not required by the outside worlds, only giving the bare minimum required by the consumer for successful use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Inheritance?

A

Using a new class to extend the definition/functionality of an existing class.

EG1: If there were several types of bank accounts, a basic Account class could be implemented, and several other classes for specific accounts can inherit from the account class. E.G. Saving Account inherits from Saving Account.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Polymorphism?

A

Changing or adding to the functionality of a class by overriding and/or overloading methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the SOLID principles?

A
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Single Responsibility Principle?

A

A class should hold a single responsibility to an actor. There should be never more than one reason for a class to change. If there is, a new class is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Open-Closed Principle?

A

Software entities should be open for extension, but closed for modification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Liskov Substitution Principle

A

Subtypes must be substitutable for their base types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Inferface Segregation Principle?

A

Classes that implement interfaces, should not be forced to implement methods they do not use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Dependency Inversion Principle?

A

High-level modules should not depend on low level modules, rather both should depend on abstraction. Abstraction should not depend on details; rather detail should depend on abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is Object Oriented code made DRY (not-repeating)?

A
  1. Never create classes or interfaces that share the same functionality as another. SOLUTION: Use the existing class, or extend with inheritance if extending functionality.
  2. Minimise extensive if statements, especially for type checking. Use polymorphism to create simpler functions, or simplify as much as possible.
  3. Strictly adhere to Liskov Substitution principle & by extension the Open-Closed principle.
  4. If a class performs an action that is already covered by another class, it is acting as a middle man, which is repetition
  5. If a class does not have a responsibility, and uses another class, integrate the two classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Loose Coupling and why is it good?

A

When a class is used inside another class, it is considered a dependency - the class depends on the existence of another class to work. Loose coupling means that the state of the class instance (object) is never dependent on another class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What traits indicate (unwanted) Tight Coupling?

A
  1. Feature Envy: A method accesses the data of another object more than its own data.
  2. Inappropriate Intimacy: One class uses the internal fields and methods of another class. Good classes should know as little about each other as possible. Such classes are easier to maintain and reuse.
  3. Message chains: When methods beget methods beget methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are good practices for method composition?

A
  1. Any section of code that does something can be encapsulated in another (private) method. This makes the code MUCH more readable to another developer.
  2. Where simple code can be obvious, let it be obvious. Don’t encapsulate simple operations for the sake of readability. Developers aren’t stupid.
  3. Minimise temporary variables that are not “used” more than once.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What type of class is a Model?

A

A model defines only raw fields of data (primary or other classes) for typing. This typically is used with database calls. These are also known as Plain Classes.

17
Q

What type of class is a DTO?

A

A Data Transfer Object is used for transferring data between api’s. This might lack fields that are handled on the back-end (i.e. ID, created-on)