Week_2 Flashcards
What is the essential class structure in Java?
public class ClassName {
Constants
Instance Variables
Constructors
Accessors and Mutators
Behaviours (Methods)
…
toString()
}
What is an instance variable?
Instance variables describe the STATE of an object; they are declared at the TOP of a class; they are initialized IN a constructor.
What is encapsulation?
It is the bundling of data and the methods (behaviours) that work on that data.
What is information hiding?
We hide HOW methods achieve their results, and instead share only that information users need to know to achieve their goals
What are visibility modifiers?
Determine what is public (accessed by other classes) and what is private (can be referenced only within the class itself).
In a class, what should be public? What should be private?
Instance variables = PUBLIC
classes and constructors = PUBLIC
Methods part of interface = PUBLIC
Methods part of implementation = PRIVATE
Constants = public or private
Local variables – implicitly private; this doesn’t have to be explicitly declared.
What are getters / accessors?
They allow us to see an object’s data when its instance variables are all private; it gives us a copy of that object’s specific variables.
public int getPrice() {
return price; // return
}
What are setters / mutators?
Allow us to modify the data of a particular object.
public void setName (String newName) {
this.name = newName; // assignment
}
What are side effects?
A side-effect is an observable, permanent change that happens IN ADDITION TO the primary job of a method. Ideally, all methods should have one and only one job; they should NOT have side-effects.
What are four examples of side-effects?
1) Modifying a non-local variable.
2) Modifying a static variable.
3) Modifying a mutable argument passed to the method.
4) Performing IO, such as printing.
What is the .toString() method?
For developers, not users; it describes the current state of the object. It’s used whenever we use System.out.pringln() on the object.
What is the THIS keyword and how is it used?
THIS refers to the object whose methods is executing.
We use THIS to invoke an alternate constructor.
We use THIS when an instance variable and parameter have the same name. (THIS will refer specifically to the instance variable.)
What is coupling?
Coupling refers to the amount of reliance that modules have on each other. We aim for MINIMAL COUPLING.