Objects Flashcards

1
Q

What is a field?

A

A variable that belongs to a class and make up the state of the class

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

What is a class?

A

Defines the states

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

What is an object?

A

An individual instance of a class

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

What is the default constructor?

A

Provided by Java by default, takes no parameters.

Called using new.

Dog bc = new Dog();

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

What is an initializing constructor?

A

Used to initialize fields other than default.

public Wizard (String n, int a) {
name = n;
age = a;
}

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

Why do you use this?

A

Using this.variableName always refers to the field with the specified name, even
if another variable is closer in scope.
This is called disambiguation (it eliminates ambiguity).

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

What is a parameterless constructor and why use it?

A

Once a constructor is defined, Java no longer provides default, but if you want to still have one without parameters, you need to write it!

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

What is a method?

A

A function that is a member of a class. May use fields of class. No need to pass a reference to the object.

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

What is encapsulation?

A

An object acts like a container, holding state and behavior in place. An object can protect access to fiends and methods using access modifiers.W

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

What are access modifiers?

A

A keyword placed before a class, method or field and determines its visibility.

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

What is an accessor?

A

Defined for each instance variable that the programmer wants
to make visible outside of the object.

Aka a getter.

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

What is a mutator?

A

Defined for each instance variable that the programmer wants to
make modifiable from outside of the object.

Aka a setter.

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

Why override toString()?

A

There’s a default, but it’s not useful.

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

What is the equals(Object) method?

A

he default implementation works exactly the same as the == operator; it only returns true if an object is compared to itself (it compares identity, i.e. the object’s address).
The author of a class may choose to write their own implementation, typically by following this pattern:
○ Since any type of object may be passed as an argument to the equals(Object) method, you
must first check the make sure that the object is an instance of the same class.
○ Java is a statically typed language: before you can access the state and behavior in the object, you must cast it into a variable of the correct type.
○ Compare each of the important fields in both
instances to see if they match.
This kind of comparison is called
“deep equality.”

@Override
public boolean equals(Object c) {
if(c instanceof Car) {
Car car = (Car) c;
return car.vin == this.vin;
} else {
return false;
}
}

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

What is the difference between actual and reference type?

A

The actual type is specified when new is
used to create the new object.

The reference type determines which state
and behavior can be used, regardless of
what the actual type is.

Object obj = new Wizard(); can’t use methods set by Wizard since its ref type is Object.

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

What is a static method?

A

A static member belongs to the class itself (not an object).
○ There is one copy of the method or field.
○ It is important to note that each individual object does not get its own copy of a static field; changes made to a static field
affect the one and only copy of that field.
● static methods exist in a static context, and can only access or use other methods or fields that are also static.
○ Trying to use non-static methods or fields will cause a syntax error.