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
Invoking the object’s method:
ObjectRefVar.methodName(arguments) e.g., myCircle.getArea()
26
The data fields can be of _____ types
reference
27
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
null
28
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
null - reference type 0 - numeric type false - boolean type '\u0000' - char type
29
Java assigns no default value to a local variable inside a ____
method
30
For a variable of a primitive type, the value is a _____ _____.
primitive literal
31
Declaring object reference variables (syntax)
ClassName objectRefVar = new Classname(); or Circle myCircle = new Circle();
32
For a variable of a reference type, the value is a reference to ___ ___ ___ ___ _____.
where an object is located ex. Circle myCircle = new Circle();
33
Define: Garbage collection
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
Define: Instance variables
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
The object on which an instance method is invoked is called a calling object. Give the syntax: Instance methods
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
If you want all the instances of a class to share data, which variable do you use?
static variables in this context static does not mean not changing this just means shared among the whole class
37
Define: Static variables
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
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 ___ ___.
static modifier.
39
To declare a static variable or define a static method, put the modifier static in the variable or method declaration as follows:
static int numberOfObjects; //static variable static int getNumberObjects() { //static method return numberOfObjects; }
40
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:
final static double PI = 3.14159265358979323846;
41
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.
Clock(){ hours = 12; }
42
Invoking the object’s method:
ObjectRefVar.methodName(arguments) e.g., myCircle.getArea()
43
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
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
An instance method can invoke an _____ or _____ method and access an ____ or _____ data field
instance static
45
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.
Instance Method instance data field
46
47
48
A variable or a method that is dependent on a specific instance of the class should be an
instance variable or method.
49
A variable or a method that is not dependent on a specific instance of the class should be a
static variable or method.
50
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 _____ ______.
instance variable instance method
51
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 from a ____.
static methods static class
52
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.
group related classes
53
Packages are divided into two categories:
Built-in Packages (packages from the Java API) User-defined Packages (create your own packages)
54
Using a Built-In Package
Syntax: import package.name.Class; // Import a single class import package.name.*; // Import the whole package
55
Using a Built-In Package Examples:
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
Visibility Modifiers - No keyword (default access)
The class, data, or method is visible to any class in the same package
57
Visibility Modifiers - Public
The class, data, or method, is visible to any class in any package
58
Visibility Modifiers - Private
The data or methods can be accessed only by declaring the class
59
To prevent direct modifications of data fields, you should declare the data fields _____, using the _____ modifier This is known as ____ _____ ____________.
private data field encapsulation
60
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:
Data may be tampered with. The class becomes difficult to maintain and vulnerable to bugs.
61
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 getter (accessor) setter (mutator)
62
A getter (accessor) method has the following signature: Example: get the radius
public returnType getPropertyName() public double getRadius()
63
A setter (mutator) method has the following signature: Example: set radius
public void setPropertyName(dataType propertyValue) public void setRadius(double radius)
64
(Socrative) Variables that are shared by every instance of a class are:
static variables
65
A method that is associated with an individual object is called:
an instance method
66
Encapsulation means:
that data fields should be hidden to prevent direct access to a client program