Unit 5 - Writing Classes Flashcards
Access modifiers
Affect the access of classes, data, constructors, and methods
keyword private
Restricts access to declaring class
keyword public
Allows access from classes outside the declaring class
In APCSA, classes and constructors are designated as
public
In APCSA, instance variables are designated as
private
In APCSA, methods can be designated as either
private or public
Computer science creates models of
Things that exist in the real world
Blueprints -> class
Using the class to create instances -> objects
Attributes of objects -> instance variables
Behaviors of objects -> methods
public Snack () { name = ""; calories = 0; }
This is classified as a
Default constructor
No values in parameter list
public Snack (String n, int c) { name = n; calories = c; } This is classified as a
Overloaded constructor
Values in parameter list
A class can have what features
Private instance variables
Public constructors - default, overloaded
Methods - accessor, mutator
Object in another class can call a public method in another class but can’t call a
PRIVATE method in another class
Encapusulation
Wrapping the data (variables & code) that acts on dat (methods) in one unit (class)
We perform encapusulation by
Writing a class Declaring instance variables as private Providing access & modifier methods
Why make instance variables private?
Restrict access to read-only
Option to provide validation checks
Object’s state
Its attributes & their values at a given time
Defined by instance variables belonging to object
Creates a “has-a” relationship between object and its instance variables
Constructors
Used to set initial state of object which should include initial values for all instance variables
Constructor used to set state depends on the way
That the object is instantiated
Only one constructor can be used to set
Initial states of instance variables
Sport tbh = new Sports();
This requires which type of constructor?
Default constructor
Sport wp = new Sport (“Water Polo”, 14);
This requires which type of constructor?
Overloaded constructor
Does all instance variables need to be set by parameters?
No
In some constructors, a portion of instance variables can be set in code body while the rest in the parameter list.
Parameters in constructors are
Local variables only to constructor to which they are sent
If no constructor is provided, Java provides a
default constructor in which all instance variables are set to default values
The default constructor provided by Java set each data type to what default values
int - 0
double - 0.0
Strings/ other - null
In starting a class,
note the instance variables, constructors, and methods
Comments
Ignored by compilers/ interpreters
Help make code more readable
Prevent execution when testing alternative code
Types of comment
// single line /* Multi line comment*/ /** Documentation *comment *to create Javadoc */
Documentation comments are found in
Javadoc
Tags can be used in Javadoc
preconditions
comments for a method
must be TRUE for a method to work -> often a guarantee about a state of parameter
postconditions
comments for a method
must be TRUE after the execution of a code -> what is outcome or state of instance variables
Code should be written to
Meet the postconditions