Basic Objects Flashcards

1
Q

What is OOP?

A

Object Oriented Programming: a programming model based on the construction and use of objects- a program built under this model can be seen as a set of objects interacting together

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

What makes up a class?

A

When you create a class, you’re actually defining a new type of data (i.e. an object)
Like any other type, a class has:
- a name (starts with an uppercase by convention
- some kind of data that is stored: a collection of variables
- some kind of actions that can be performed on the data: a collection of methods

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

What are instance variables?

A

The object’s data: each instance will have its own specific set of values for the instance variables

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

What do variables of an object type contain?

A

A reference, not the object itself: just like we have seen for arrays and Strings, because they are objects as well

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

What are instance methods?

A

methods that can only be used on instances. These methods have access to the instance variables of that specific instance (static keyword is omitted)

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

Why do we use the ‘this’ keyword?

A

In an instance method, if you have a local variable with the same name as an instance variable, the local one will be used. You use the keyword to represent the current instance of the object.

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

What is the toString() method?

A

public String toString()

This method is automatically called by Java to get a String representation of your object (called when printing or concatenation is needed with your object)

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

What are constructors?

A

A special method that is used to instantiate an object. We normally use it to initialize the instance variables. They have no return type at all (not even void) and must have the exact same name as the class. You can define multiple constructors so long as they have different signatures

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

What is the purpose of the public keyword?

A

makes the method/variable accessible outside of the class

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

What is the default constructor?

A

Provided by Java in case no constructor is defined in a class. Does not do anything except instantiate the object. It disappears as soon as one constructor is defined in the class. This means that you cannot use a constructor with no parameters if you declared only one constructor with parameters

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

What is the distinction between functions and methods?

A

functions are static, methods work with instance variables because they are not static

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

What are the access modifiers?

A

public, private, protected-private

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

What do public and private mean?

A

public: means any code, anywhere, can access or use it
private: means only methods in this same class can access or use it

use private for instance variables and public for most instance methods

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

What is encapsulation?

A

The idea that you can restrict access to some of the object’s fields. It protects the internals, preventing other classes from misusing the object.

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

What are fancy names for get/set methods?

A

accessor/mutator

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

What is a mutable object?

A

An object whose contents can be changed after the object is constructed. That is, it has a mutator method.

17
Q

What happens when you pass a mutable object to a method?

A

The method can change the contents of the object. the change is permanent: the object stays changed after the method ends. In Java, this only happens with mutable objects, not primitives

18
Q

What are immutable objects?

A

An object whose contents cannot be changed after the object is constructed. It has no mutator or equivalent methods, and all instance variables are private. Only write setter methods if you need them

19
Q

Are Strings mutable or immutable?

A

Every string is immutable. Once it’s created you cannot change it’s value. Every time you “modify” the value of a String variables, a new string object is created and the new reference to it is returned

20
Q

What are class variables and methods?

A

Variables and methods which do not refer to any one specific instance but belong to the class as a whole. To create them, we need to use the static keyword

21
Q

What are some standard methods for comparing the actual data inside Objects?

A

object1.equals(object2)
object1.compareTo(object2)
- gives a negative value if object1 is “smaller
- gives a positive value if object1 is “larger”
- gives a zero if they are “equal”

you can write these methods for your own objects. the default ones are not useful