Java SE 11: Encapsulation Flashcards
static is a ______ that we apply to a method or a variable
modifier
_____ means that the method or variable belongs to the class and is shared by all objects in that class
static
a static modifier means it’s not unique to an object instance, and it can be accessed without _________ the class. So we don’t need to use the ___ keyword, and no constructor gets called when we access static methods or variables
a static modifier means it’s not unique to an object instance, and it can be accessed without instantiating the class. So we don’t need to use the NEW keyword.
A constructor gets called when we access static methods or variables.
True or False
False
You don’t need to use the new keyword, and no constructor gets called when we access static methods or variables
An _______ vairable is unique to an individial object
Instance
In the following ItemSizes class does a ItemSizes object need to be instatied in order to access it’s members?
Yes or No
public class ItemSizes {
public static String mSmall = “Mens Small”
}
}
No
since the member is static it can be accessed without having to create the ItemSizes class
an ______ _____ is a non-static method
instance method
Is the setSize() method an instance method or static method?
public class Item {
public String size;
public void setSize(String sizeArg) {
size = sizeArg;
}
}
Instance method
How do you make a variable a constant?
using the final keyword before the variable type name
ex. final int COLUMNS
constant variables can be changed after they have been initialized.
True or False
False
Constants cannot be changed after they have been initialzed.
The name of variables declared class ______ should always be uppercase
constants
What is called when the compiler changes the type of variable to a type that supports a larger size?
Promotion
Some promotions are done automatically by the compiler
True or False
True
Feature of promotion is to avoid ________ values.
Truncated
We use ____ _______ to lower the range of a value
type casting
Type casting a value has no effect on the amount of memory the value consumes.
True or False
False
Type casting reduces the amount of memory used by the value. Ex. change and long value to an int
is the example below a promotion or type casting?
int i = 3;
long l = i;
double d = 3;
Promotion
In the example below will the num3 value be truncated?
Yes or No
int num1 = 55555;
int num2 = 66666;
long num3 = num1 * num2;
Yes
In the example below the result num3 is 3.7 billion which is too large to fit in an int type. What do i need to change to ensure the final number is truncated.
int num1 = 55555;
int num2 = 66666;
long num3 = num1 * num2;
Either num1 or num2 needs to be changed to a long type.
How would you convert the number represented as a string to a primitive type?
String sNum = “150”;
int intNum = ________________ ;
int nNum = Integer.parseInt(sNum);
How would you convert the boolean represented as a string to a boolean type?
String nNum = “true”;
boolean b = ______________;
boolean b = Boolean.parseBoolean(sNum);
How would you modify the code below to make the setID method private but still accessible?
public class item {
print int id;
print String desc;
public item () {
desc= “–description required–”;
}
public void setId() {
id = Item.nextId++;
}
public class item {
print int id;
print String desc;
public item () {
setID();
desc= “–description required–”;
}
private void setId() {
id = Item.nextId++;
}
In encapsulation, the variables of a class will be _____ from other classes, and can be accessed only through the methods of their current class.
Hidden
Encapsulation uses access control to hide the fields, so we can provide safe access through ______ and ______ methods.
….. getter and setter methods.
Encapsulation encourages good _____ ______design
object-oriented
If you make attributes private, how can another object access them?
One object can access the private attributes of a second object. If the second object provides public methods for each of the operations that are to be performed on the value of an attribute.
IE. Getter and setter methods
The below is an example of a Setting Method with ________
public void setColorCode(char newCode) {
if (newCode == ‘R’) {
colorCode = newCode;
return;
}
if (newCode == ‘G’) {
colorCode = newCode;
return;
}
if (newCode == ‘B’) {
colorCode = newCode;
return;
}
System.out.println(“Invalid colorCode. Use R, G, or B”);
}
Checking