Chapter 9 Objects and classes Flashcards

1
Q

Programming using objects is known as?

A

Object-oriented programming (OOP) involves programming using objects.

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

What does an object represent?

A

An object represents an entity in the real world that can be distinctly identified.

An object has an
- identity
- state,
- behaviours.

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

The state of an object consists of what?

A

The state of an object consists of a set of data fields (also known as properties) with their current values.

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

The behaviour of an object is defined by what?

A

A set of methods.

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

The state(also known as its properties or attributes) of an object is determined by:

A

methods

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

Define classes

A

Classes are constructs that define objects of the same type.

A class is a template, blueprint, or contract that defines what an object’s data fields and methods will be.

An object is an instance of a class. You can create many instances of a class. Creating an object or an instance is referred to as instantiation.

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

Constructors definition

A

A class uses variables to define data fields and methods to define actions.

A class also provides methods of a special type, known as constructors, which are invoked to create a new object.

A constructor has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.

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

Constructors syntax

A

Syntax:
class ClassName {
ClassName(parameter list) {
//constructor body
}
}

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

The illustration of class templates and objects can be standardized using

A

Unified Modeling Language (UML) notation.

This notation is called a UML class diagram, or simply a class diagram.

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

In the UML class diagram, the data field is denoted as (syntax)

A

dataFieldName: dataFieldType

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

In the UML class diagram, the constructor is denoted as:

A

ClassName(parameterName: parameterType)

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

In the UML class diagram, the method is denoted as (syntax):

A

methodName(parameterName: parameterType): returnType

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

A UML Class Design consists of

A

class name
data fields
constructors and methods

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

A constructor with no parameters is referred to as a

A

no-arg constructor

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

Constructors must have the same name as

A

the class itself

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

Constructors do not have a

A

return type—not even void

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

Constructors are invoked using the

A

new operator when an object is created

ex. you have to use the new keyword when invoking a new array.

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

A constructor can perform any action, but constructors are designed to perform

A

initializing actions, such as initializing the data fields of objects

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

T or F. Constructors can be overloaded

A

T

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

Constructors are used to construct objects. To construct an object from a class, invoke a constructor of the class using the new operator, as follows:

A

new ClassName(arguments);

or new Circle();

or new Circle(5.0);

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

Define: a default constructor

A

A class may be defined without constructors.

In this case, a no-arg constructor with an empty body is implicitly defined in the class.

This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class.

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

A class is essentially a programmer-defined type. A class is a reference type, which means that a variable of the class type can reference an instance of the class.

Newly created objects are allocated in _____ memory and objects are accessed via the object’s _____.

A

heap

reference variables

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

After an object is created, its data can be accessed and its methods can be invoked using the ___ ________, also known as the object member access operator:

A

dot operator (.)

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

Referencing the object’s dataField:

A

objectRefVar.dataField

e.g., myCircle.radius

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

Invoking the object’s method:

A

ObjectRefVar.methodName(arguments)

e.g., myCircle.getArea()

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

The data fields can be of _____ types

A

reference

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

If a data field of a reference type does not reference any object, the data field holds a special literal value, ___.

A ____ value means that the reference variable it is not holding a reference to an object in memory

A

null

28
Q

The default value of a data field is ____ for a reference type, ___ for a numeric type, ____ for a boolean type, and _____ for a char type.
However, Java assigns no default value to a local variable inside a method

A

null - reference type

0 - numeric type

false - boolean type

‘\u0000’ - char type

29
Q

Java assigns no default value to a local variable inside a ____

A

method

30
Q

For a variable of a primitive type, the value is a _____ _____.

A

primitive literal

31
Q

Declaring object reference variables (syntax)

A

ClassName objectRefVar = new Classname();

or Circle myCircle = new Circle();

32
Q

For a variable of a reference type, the value is a reference to ___
___ ___ ___ _____.

A

where an object is located

ex. Circle myCircle = new Circle();

33
Q

Define: Garbage collection

A

The garbage collector is a program which runs on the Java Virtual Machine which gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.

34
Q

Define: Instance variables

A

The data field radius in the Circle class is known as an instance variable.

An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class.

35
Q

The object on which an instance method is invoked is called a calling object. Give the syntax: Instance methods

A

The .getArea() method in the Circle class is an instance method because you can invoke it only on a specific instance.

objectRefVar.methodName(arguments)

Example:
Circle myCircle = new Circle();
double area = myCircle.getArea();

36
Q

If you want all the instances of a class to share data, which variable do you use?

A

static variables

in this context static does not mean not changing

this just means shared among the whole class

37
Q

Define: Static variables

A

Static variables store values for the variables in a common memory location.

Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected.

Java supports static methods as well as static variables

38
Q

Static methods can be called without creating an instance of the class

Static variables are shared by all the instances of the class.

Static methods are not tied to a specific object.

Static constants are final variables shared by all the instances of the class.

To declare static variables, constants, and methods, use the ___ ___.

A

static modifier.

39
Q

To declare a static variable or define a static method, put the modifier static in the variable or method declaration as follows:

A

static int numberOfObjects; //static variable
static int getNumberObjects() { //static method
return numberOfObjects;
}

40
Q

Constants in a class are shared by all objects of the class. Thus, constants should be declared as final static.
For example, the constant PI in the Math class is defined as follows:

A

final static double PI = 3.14159265358979323846;

41
Q

You are given a class named Clock that has one int instance variable called hours. Write a constructor with no parameters for the class Clock. The constructor should set hours to 12.

A

Clock(){
hours = 12;
}

42
Q

Invoking the object’s method:

A

ObjectRefVar.methodName(arguments)

e.g., myCircle.getArea()

43
Q

Copying variables

assign circle c1 with a radius of 5
assign circle c2 with a radius of 9
copy the value of c1 into c2

A

Circle c1 = new Circle(5);
Circle c2 = new Circle(9);
c1 = c2;

The initial radius 5 of object c1 has gone into the JVM “garbage collection”

44
Q

An instance method can invoke an _____ or _____ method and access an ____ or _____ data field

A

instance

static

45
Q

A static method can invoke a static method and access a static data field.

However, a static method cannot invoke an ___ ___ or access an ____ ___ ___, since static methods and static data fields don’t belong to a particular object.

A

Instance Method

instance data field

46
Q
A
47
Q
A
48
Q

A variable or a method that is dependent on a specific instance of the class should be an

A

instance variable or method.

49
Q

A variable or a method that is not dependent on a specific instance of the class should be a

A

static variable or method.

50
Q

For example, every circle has its own radius, so the radius is dependent on a specific circle. Therefore, radius is an ____ ____ of the Circle class. Since the getArea method is dependent on a specific circle, it is an _____ ______.

A

instance variable

instance method

51
Q

None of the methods in the Math class, such as random, pow, sin, and cos, is dependent on a specific instance. Therefore, these methods are _____ _____. The main method is ____ and can be invoked directly froma ____.

A

static methods

static

class

52
Q

A package in Java is used to _____ _____ _____. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code.

A

group related classes

53
Q

Packages are divided into two categories:

A

Built-in Packages (packages from the Java API)

User-defined Packages (create your own packages)

54
Q

Using a Built-In Package

A

Syntax:

import package.name.Class; // Import a single class

import package.name.*; // Import the whole package

55
Q

Using a Built-In Package Examples:

A

import java.util.Scanner;
//Import the Scanner class from the java.util package

import java.util.*;
//Import ALL the classes in the java.util package:

56
Q

Visibility Modifiers - No keyword (default access)

A

The class, data, or method is visible to any class in the same package

57
Q

Visibility Modifiers - Public

A

The class, data, or method, is visible to any class in any package

58
Q

Visibility Modifiers - Private

A

The data or methods can be accessed only by declaring the class

59
Q

To prevent direct modifications of data fields, you should declare the data fields _____, using the _____ modifier

This is known as ____ _____ ____________.

A

private

data field encapsulation

60
Q

The data fields in the Circle class can be modified directly (e.g., c1.radius = 5 or Circle.numberOfObjects = 10). This is not a good practice—for two reasons:

A

Data may be tampered with.

The class becomes difficult to maintain and vulnerable to bugs.

61
Q

A private data field cannot be accessed by an object from outside the class that defines the private field. However, we often need to get or modify a data field.

To make a private data field accessible, provide _____ method to return its value.

To enable a private data field to be updated, provide a _____ method to set a new value.

A

a getter (accessor)

setter (mutator)

62
Q

A getter (accessor) method has the following signature:

Example: get the radius

A

public returnType getPropertyName()

public double getRadius()

63
Q

A setter (mutator) method has the following signature:

Example: set radius

A

public void setPropertyName(dataType propertyValue)

public void setRadius(double radius)

64
Q

(Socrative) Variables that are shared by every instance of a class are:

A

static variables

65
Q

A method that is associated with an individual object is called:

A

an instance method

66
Q

Encapsulation means:

A

that data fields should be hidden to prevent direct access to a client program