Object-Oriented Programming Flashcards

1
Q

What is OOP?

A

A STYLE of programming centered around objects rather than functions.

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

What OOP are there?

A

C#, Java, Ruby, Python, JavaScript (controversial)

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

What relevance does OOP have to frameworks?

A

A lot of frameworks were designed with OOP in mind. E.g. Angular

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

What is an object?

A

An object is a combination of related variables (properties) and functions(methods) grouped together into a unit.

It is an instance of a class that has attributes, behaviour and identity and is defined by the class definition. E.g. class named Vehicle and objects could be Van, Truck, Car

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

What are the 4 pillars of OOP?

A

Encapsulation, Abstraction, Inheritance & Polymorphism

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

What is encapsulation?

A

Encapsulation is the grouping together of the related variables and functions.

When the functions exist inside the object, we don’t need to pass it any parameters, because the parameters are all modelled as properties of the object.

Simply, this means they exist within the object itself, therefore do not need to be passed into the function.

E.g.

let employee = {
baseSalary: 30_000,
overtime: 10,
rate: 20,

getWage: function( ) {
  return this.baseSalary + (this.overtime * this.rate);
} };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the benefit of encapsulating the functions and variables?

A

Reducing complexity of your programme & increasing the reusability of your objects.

Fewer parameters need to be passed. This means that your code will be more easily maintainable.

“The best functions are those with no parameters!” - Uncle bob

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

What is abstraction?

A

Hiding the complex details (implementation) & expose only the essentials )

We do this by isolating the properties and methods.

  1. ) simpler object interface (we can use and understand the object with just a few properties and methods)
  2. ) reduces the impact of change - changing things inside the object will not leak effects outside, because no code outside of the object touch those methods

E.g a DVD player

  • we press play and don’t care what happens on the inside
  • the complexity is hidden from us, inside the DVD player, this is abstraction in practice
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is inheritance?

A

Instead of redefining common properties and methods for each object, they can be defined once in a generic object and then inherited by other objects.

It allows us to re-use code easily & eliminate redundant code.

Reduces opportunity for bugs.

Makes it easier to change/maintain code as you just need to look in one place

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

What is polymorphism?

A

A technique that allows you to get rid of long if/else & switch statements. This is basically refactoring.

Poly means many
Morph means form
= many forms

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

What are functions in OOP?

A

Functions in OOP are objects!

The purple cubes in VSC are methods.

The blue rectangles in VSC are properties.

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

How can we add properties to an object?

A

There are two ways we can add properties:

1.)We can generate a new property via the dot notation.

E.G. myObject.newProperty = _____;

2.)Using bracket notation

E.G. myObject [ ‘newProperty’ ] = ____;

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

How can we access properties?

A

Either with . or [ ] notation.

dot . notation is best, but bracket notation can be useful for property names that are not valid identifiers such as special characters

E.G. myObject[ ‘object-property’ ]

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

How can we delete properties?

A

By using the keyword ‘delete’

E.G. delete myObject.myProperty

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

How can we iterate over an object?

A

1.) FOR…IN loop. This loops over the object regardless of length and we can access both the key & value

for ( let key in circle ) {
console.log( key, circle[key])
}
the above would output the key:value pair

2.) using Object.keys

const keys = Object.keys(circle)
     console.log(keys);

the above would output all keys into an array WITHOUT the values

3.) using the IN operator to iterate over the properties

if ( ‘radius’ in circle )
console.log( ‘Circle has a radius.’ );

= if the circle object has a property of radius console log the message

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

What is the difference between scope & closure

A

In scope, the variables are temporary.
E.G.
- a function within a function
- child function has two variables x & y
- variables are re-created & re-initalised each time the child function is called
- after the function has completed, the variables in the child function die in the scope

In closure, the variables within the parent function stay initialised

17
Q

How can we implement abstraction?

A

By creating private properties & methods
( having less methods and properties available within our object )

We can do this by declaring local variables and functions using LET instead of using ‘this.’ (which is what gives access to methods & props)

18
Q

What is a prototype?

A

Every object in JS, except the root object (parent) has a prototype .

Think of it as a starting base set up for all objects

E.G the array prototype would contain predefined methods e.g. .filter(), map(), indexOf(), accessible on any other array you create

19
Q

What is Prototypical Inheritance?

A

Objects inherit everything defined in the parent prototype.

When we call a method on an object, JS will go up the prototypical inheritance ladder one by one by one if it cannot find the method on the object we’ve called it on.

It will keep climbing toward the parent until it finds it.

OBJECTS CREATED BY A GIVEN CONSTRUCTOR WILL HAVE THE SAME PROTOTYPE.

20
Q

What is Multi-level Inheritance?

A

myArray => arrayBase => objectBase (because array is essentially an object!)

Or if we created our own constructor function for objects:

circle => circleBase (custom constructor) => objectBase

21
Q

How can we add a prototype member to an object

A

Circle.prototype.draw = function() {
console.log(‘draw’)
}

this means we don’t have to have the draw method loaded into every object, but we can still access the method because it lives in the prototype of circle.

22
Q

What is a class?

A

A class describes all the attributes & methods that implement the behavior of member objects.

It is a comprehensive data type, which represents a blue print of objects. It is a template of object.

A class can be defined as the primary building block of OOP. It also serves as a template that describes the properties, state, and behaviors common to a particular group of objects.

23
Q

How can we create a new instance of a class?

A

By using the new operator (keyword).

24
Q

What is a constructor?

A

A special method of a class, which is called automatically when the instance of a class is created.

It is created with the named class and initialises all class members (objects of that class).