Classes and Objects (Head First Java - Ch. 2, 4, 10) Flashcards

Head First Java - Ch. 2, 4, 10

1
Q

Whats a primitive data type? give examples.

A

not considered objects or raw values and are stored directly on STACK.

byte, int, float, char, long, short, double, boolean

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

How many classes in a file can be a public class?

A

Only one.

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

Constructors are a special kind of method. What are three peculiarities?

A

A constructor must have the same name as the class itself.

Constructors do not have a return type, not even void.

Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

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

__ is a construct that defines objects of the same type

A

A class

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

An object is an instance of a __

A

a class

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

Which operator is used to create an object?

A

“new” operator

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

You need ____ classes to create and use an object? Why?

A

two classes. One for the type of object you want to use. One to put your main() method in (tester class)

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

What is the point of a tester class?

A

To create and access objects of your new class type. It will use the dot operator to access the methods and variables of the new objects

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

How do we declare an object in the tester class (an instance of a class)? How do we access the variables method?

A
  1. [Class name] [variable] = new Class name
  2. [variable].[method]

method is made in other class

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

What’s a parameter?

A

is a local variable, with a type and name, and can be used inside of the body of the method

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

What is a return type?

A

the expected data type in return value for a method. A method could also have a void return type.

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

Can a variables value change once passed into a method?

A

No - the value can only be changed within the method

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

Is it required to have a return value for a non-void return type?

A

No it’s not require if we are just calling the method for the work it does inside the method

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

What’s a setter method?

A

A method used just to set the value of an instance variable - it will have a void return type

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

What’s a getter method?

A

A method used to return the value of an instance variable - it will have a matching return type

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

What is encapsulation and whats it relation with setter methods?

A

Encapsulation is using setter methods to set boundaries for values. For example, a setter method can contain code to avoid negative values.

17
Q

instance variables should be ______

getters and setters should be ______

A

instance var should be private.

getters and setter should be public.

18
Q

What is the default value for an instance variable?

A

integers = 0
floating points = 0.0
booleans = false
references (String or objects) = null

19
Q

What’s the difference between a local and instance variable?

A

Instance variables are declared inside a CLASS, and has a default value

Local variables are declared within a METHOD, and don’t have a default value. They must be initialized.

20
Q

What can you not do with the Math class, but you can do with every other class?

A

We cannot create an instance of the class Math. (Can’t be instantiated) It is marked private.

21
Q

What is a static method?

A

a method part of a class rather than the instance of a class. No instance variables are required

22
Q

What happens if you try to use an instance variable in a static method?

A

Error. The static function won’t know which variable is being referred to

23
Q

What type of value is the same for ALL instances of classes?

A

Static variable. It will give you one value per CLASS, instead of one value per INSTANCE

24
Q

How do we make a variable a constant? (make it so a variable can’t be changed)

A

We can write:
public static final [var. type] [var. name]

static {
[var.name] = value;
}

25
Q

Constant “variable” names should be in _______.

A

ALL CAPS

26
Q

A final variable means _________

A

you can’t change its value

27
Q

A final method means _________

A

you can’t override the method

28
Q

A final class means _________

A

you can’t extend the class (can’t make a subclass)

29
Q

What is override?

A

When you have two methods with the same method name and the same parameters

30
Q

What is overloading?

A

When you have two methods with same method name, and different parameters

31
Q
A