Chapter 3 - Classes some basics Flashcards

1
Q

what is a class in terms of type

A
- A class is a special kind of programmer-defined type,
and variables can be declared of a class type
- A value of a class type is called an object or an
instance of the class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

primitive vs class type values

A
  • prim -sinlge piece of data
  • classes - multiple pieces of data as well as methods
  • All instances of a class have the same methods
    – All instances of a class have the same pieces of data (i.e.,
    name, type, and number)
    – For a given instances, each piece of data can hold a
    different value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are the contents of class definition?

A
  • specifies that data items and the methods that all of its objects will have
  • data items - fields - instance variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to invoke a method

A

use the calling object and the mthod name

classVar.mymethod();

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

two method categories

A
  • perform action - dont return, void

- compute and return a value

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

keywords used for methods

A

typeReturned methodname

void
main

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

turning a method into a void method

A

objectName.returnedValueMethod()

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

local variable

A

variable declared within a method definition

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

examples of local variables

A
  • declared in the main method
  • method parameters
  • If two methods each have a local variable of
    the same name, they are still two entirely
    different variables
  • for loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

formal parameters

A

some methods need additional data via a list of parameters in order to perform their work

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

what does a parameter list do

A

A parameter list provides a description of
the data required by a method
– It indicates the number and types of data
pieces needed, the order in which they must be
given, and the local name for these pieces as
used in the method

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

what are arguments

A

actual parameters
when a method is invoked, the appropriate values must be passed in the form of arguments
number and order must match that of the para list
type of each argument must be compatible with that of the corresponding parameter

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

call by value mechanism

A

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.

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

pass by reference vs pass by value

A

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value.
Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.

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

what happens if arguments and parameters dont match

A

java will try an automatic type conversion

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

what is a constructor?

A

A constructor is a special kind of method that
is designed to initialize the instance variables
for an object:

17
Q

the this keyword in constructors

A

this refers to the instance variables e..g this.word (instance variable word) = word

18
Q

what is overloading?

A

when two or methods in the same class have the same name - to be valued they must have different signatures(i.e. their parameter list must differ) - they cant have different return types

19
Q

what are the default variable declarations

A
boolean - false
class type - null
string - ""
20
Q

accessor and mutator methods

A

public String getString(){return String;}

public void setName(){this.name = name;}

21
Q

describe an encapsulated class

A

implemented details hidden in the capsule
-private instance variables
-private constants
-private methods and bodies f public and private method definitions
interface available to a programmer using the class :
- comments
-headers of methods
-public defined constants
a class definition should have no public instance variables

22
Q

public vs private modifiers

A

public - no restrictions on where an instance variable or methods can be used
private - instance variable or method cannot be accessed by name outside the class, private methods are typically helper methods

23
Q

each class typically needs to have its own….methods

A

toString and equals

24
Q

example of a toString method

A

public String toString(){return “Name:”+”name”}

25
Q

example of an equals method

A

publicbooleanequals(ObjectanObject){if (this==anObject){ //are the two objects in the same memory location?returntrue;}

//another part of the method must be written to check that the thing is equal but not necessarily in the same memory location