Classes and Objects 2 Flashcards

1
Q

What are getters and setters?

A

Methods that generally initialising/updating/accessing instance variables

aka Accessor/Mutator

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

What are the default values when you create a new object?

A

The initial value of the instance variables are set to default values based on the data type

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

What are the cons of initializing objects with default values?

A

You would need 100 lines to call create value for each attribute

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

What is the definition of constructor?

A

A method used to create and initialise an object

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

What are the features of a constructor?

A
  • The right hand side calls a class’ constructor
  • Constructors are methods
  • Constructors are used to initialize objects
  • Constructors have the same name as the class
  • Constructors cannot return values
  • A class can have one or more constructors, each with a different set of parameters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the definition of method overloading?

A

Ability to define methods with the same name but with different signatures (argument types and/or numbers)

  • This is a form of polymorphism (same method - different behaviour)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the definition of polymorphism?

A

Ability to process objects differently depending on their data type or class

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

How are methods with the same name distinguished?

A
  • number of arguments
  • type of arguments
  • position of arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the definition of “this”?

A

A reference to the calling object, the object that owns/is executing the method

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

What is the definition of instance variable?

A

A variable in an instance of an object

example: CentreX, CentreY

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

What is the definition of static variables?

A

A variable that is shared among all objects of the class; a single instance is shared among all objects of the class. Such an attribute using the class name

example: numCircles

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

What is the definition of static members?

A

Methods and attributes that are not specific to any object of the class

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

What is the definition of Static Method?

A

A method that doesn’t depend on (access/modify) any instance variables of the class. Such a method is invoked (called) using the class name.

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

What are the constraints of static methods?

A
  • Static methods can only call other static methods
  • Static methods can only access static data
  • Static methods cannot refer to Java keywords such as, this or super because they are related to objects (class instances)
  • Do not make all methods and attributes in your classes static
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to decide to make an attribute or a method static?

A

Consider whether it is a class level member or an instance specific member

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

What does the equals method do?

A

It compares two objects to see if they are equal

17
Q

Why do we use the equals method?

A

Using “==” will only check if references are equal as opposed to checking if objects are equal

18
Q

What do we use the toString method for?

A

It is used to print the strings of the object

19
Q

What does the copy constructor do?

A

It creates a separate copy of the object sent as input