OOP Flashcards

1
Q

What is OOP?

A

Object Oriented Programming is a style of programming that uses objects to represent data in your code.

In most OOP languages, like Java and C#, we use classes to represent data. Classes are like JavaScript objects with super powers.

The four pillars are inheritance, abstraction, encapsulation, and polymorphism

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

What is abstraction?

A

Abstraction refers to a simple interface that does a complicated thing.

Example: you have a button to turn your cell phone on and off. You don’t need to know the cell phone works, or what happens under the hood when you press it. You just need to know how to press it. The engineers gave you a simple interface (a button) to do a complex task (turning on your phone).

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

What is encapsulation?

A

Encapsulation refers to hiding data in private fields so that it’s harder to mess up.

Example: let’s imagine that you represented a person in code. You might represent it as an object. In JavaScript, that object might look like:

const johnDoe = {
name: “John Doe”,
age: “45”,
SSN: “777-77-7777”
}

A person’s name and age might change, but their social security never will. It would be nice if we had a way to protect the SSN property from ever changing so we don’t accidentally overwrite it or modify it later. In JS, that’s tricky. In most OOP languages, it’s very easy. In C# or Java, for example, we can declare any field “private” so it can’t be overwritten or accessed by the rest of the code.

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

What is inheritance?

A

Inheritance refers to one object acquiring properties and methods of another object. Most OOP languages use classes to represent data, which are basically objects with super powers. In JavaScript we used normal objects, so we’ll stick with that language for now.

Example of inheritance: we might be tasked with making objects to represent tigers, lions, and cheetahs at a zoo. All of those objects have a lot in common– they all have names, weights, running speeds etc. Rather than writing all of those properties out three times, we might make a bass object (or class) called “largeCat” that could contain all of the stuff they have in common. Then you could make distinct objects for different types of large cats that INHERIT those common properties from your base object object. This cuts down the code we have to write and makes our code more reusable.

In JavaScript, we could do this with a mechanism called Object.create(). In most OOP languages, it’s much simpler.

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

What is polymorphism?

A

Polymorphism means “many forms”. In OOP languages, classes and methods can occur in many forms.

In our example about the zoo, we had a base object that represented a large cat and then three distinct objects representing lions, tigers, and cheetahs. While all of our cats have certain things in common, they all require different food. In OOP languages, we can define a method called “eat” on our base class and then implement it differently on each distinct child class. On the tiger class, we could have an “eat” method that accepts a parameter of “water buffalo”. On lions, the “eat” method could accept a parameter of “zebra” and so on. The “eat” method occurs in many forms, which is an example of polymorphism.

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

What’s the difference between a property and a value?

A

A property is a piece of information inside an object. In the following object, age and height are properties. 27 and “5’10” are values.

const liamObject = {
age: 27,
height: “5’10”
}

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

What’s a method?

A

A method is a function that’s defined inside an object. Methods are used to determine how an object behaves. In the following example of a JavaScript object, sayHello is a method.

const liamObject = {
age: 27,
height: “5’10”,
sayHello: function(){
console.log(“Hello, my name is Liam”.)
}
}

We can call our method like this:
liamObject.sayHello()
// Expected output: “Hello, my name is Liam.”

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

What’s a class in C#?

A

A class is a blueprint for an object that defines what that object will look like. Think of it like a cookie cutter. You define classes with properties and methods, and then you can instantiate them as many times as you want (i.e. using the cookie cutter to make as many cookies as you want.)

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