Chapter 5--creating Classes & Methods Flashcards

(21 cards)

0
Q

Variables are considered instance variables if _____

A

They are declared outside a method definition but are NOT modified by the static keyword.

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

How do you create a class variable?

A

static int VERN;

static final int YAR=10;

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

How do you create a new class?

A

with the extends keyword. e.g.,

class Buttknocker extends Fartknocker {

int Vern;
float Farts;
}

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

What is overloading methods? What makes it work or not work?

A

It is creating methods that have the same name. They must differ in the number and type of arguments. The names of the arguments do not matter.

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

Can a constructor with no arguments be called in a class that already has a constructor that accepts one or more arguments?

A

Only if a constructor with no arguments has been defined in that class

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

What are the basic differences between methods and constructors?

A

Constructors

  1. Always have the same name as the class
  2. Don’t have a return type
  3. Can’t return a value in the method by using the return statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a method’s signature?

A

It’s name and argument list

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

How do you override a method?

A

Create a method in a subclass that has the same signature as a method in a superclass.

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

If a source code file contains more than one main(), what happens?

A

Netbeans asks which one should be run

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

How do you call a superclass method that has already been overridden?

A

with the super keyword, i.e.,

super.methodname()

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

Can constructors be overridden? Why or why not?

A

No, because they always have the same name as the current class.

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

How do you call a constructor from the superclass instead of the one from the current class? What are the rules for this?

A

super()

it must be the first statement in the constructor definition.

Also, there must be a constructor with that signature in the immediate superclass.

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

Is it better to create your own constructors, or to pass them up the hierarchy? Why?

A

Pass them. You don’t always know everything a constructor is doing re: initializing.

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

Use the extends keyword in an example. What does it do?

A

Creates a subclass

class Vernyverns extends Verns{
    //body of the class

Vernyverns is the subclass, Verns sic the superclass

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

Create a class with examples of instance variables

A
class Buttnuggets extends ButtEffluvia{
    String smell;
    String texture;
    float viscosity;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create a method that does not have a return value?

A

Use the void keyword

16
Q

How do you refer to the object itself in the body of a method definition?

A

With the this keyword

17
Q

What are the possible return types in a method definition?

A

Void, a class, or a primitive data type

18
Q

What do the parameters in a method signature become?

A

Local variables in the body of the method definition. They receive their values when the method is called

19
Q

What is required with a value-returning method?

A

The return keyword inside the method definition

20
Q

Can you use the this keyword for a class method?