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
Non-void methods
Returns a single value
Header includes the return type in place of keyword void
Return expression compatible with return type is evaluated & copy of that value is returned
Accessor methods
Allow safe access to instance variables
Refer to as get methods
In order for a different class to access the instance/static variables, you need
accessor methods
Conditions of accessor methods
must be PUBLIC
return type must MATCH the type of instance variable to be accessed
name is often getNameOfVariable
no parameters
public int getCalories () {
return calories;
}
this is an example of
accessor method
toString method
Overridden methods that is included in classes to provide a description of a specific object
Generally includes what values are stored in instance data of object
If System.out.print or System.out.println is passed an object,
that object’s toString method is called and returned String is printed
public String toString()
Always returns a string
No parameters
When System.out.println (object) is called on an object in a different class
the toString method is called
returned String is printed
Void method
Does not return a value
Heading contains keyword void before method name
mutator method
Void method that changes the values of instance variables or static variables
Referred to as set methods
Allow the change of values for instance variables outside of class
What is necessary if there is any need for a different class to modify the instance variables?
Mutator method
Conditions of mutator method
Must be PUBLIC
return type must be VOID
name is often setNameOfVariable
parameter type must match the type of instance variable to be modified
public void setName(String n);
this is an example of
Mutator methods
Accessors and mutator methods can be simplified into
object name.setAge(18);
OR
object name.getAge();
object name. method(parameters if needed);
Methods can only access the private data and methods of a parameter that is a reference to an object when
the parameter is the same type as method’s enclosing class
non-void methods with parameters receive values
through parameters, use these values, and return a computed value of specified type
Class
template that defines the data through instance variables and behaviors through methods of an object
Object
Instance of a class
Method header
Consists of 5 parts Access level Ownership Return type Identifier Parameter list
Access level
set by access modifier
can be public or private
Ownership
Set by whether or not static is needed
Return type
Data type of values returned by method
can be primitive, reference, void
Identifier
name of method should be meaningful
Parameter list
enclose in parentheses, states the data type & identifier for each parameter used in method
Parameter
Information needed by method to complete its task
If methods does not use parameters,
parentheses are still needed but left empty
Instance of a class is considered mutable if the class contains a
mutator method
Good practice When actual parameter is a primitive value. formal parameter is
initialized with copy of that value
Changes to formal parameter have
no effect on corresponding actual parameter
Static methods
Associated with class, not objects of class Include keyword static in header before method name
Static methods are not able to
access or change values of instance variables
Unable to use class’ instance variable or call non-static method
static variable
Belong to class with all objects of a class sharing a single static variable Can be designated as either public or private Designated with static before method name Used with class name & dot operator
Local variable
Can be declared in body of constructors & methods
May only be used within constructor and method
Can’t be declared to be public or private
When there is a local variable with same name as instance variable, variable name will refer to
local variable instead of instance variable
Formal parameters and variables declared in a method and constructor can only
be used within that method or constructor
Method decomposition
Programmer breaks down a large problem into smaller sub problems by creating methods to solve each individual sub problem
keyword this
Within a non-static method or constructor, a reference to current object whose method/ constructor is being called
Can be used to pass current object as an actual parameter in a method call
If a class’s data and behavior are unknown, it may not be
appropriate to use this keyword
When designing a class, programmers make
decisions about what data to make
accessible
and modifiable from an external class
Data can be either
accessible or modifiable, or it can be
both or neither.
When a mutable object is a constructor parameter, the instance variable should be
initialized with a copy of the referenced object.
In this way, the instance variable is not …
,and methods are
prevented from …
an alias of the original object
modifying the state of the
original object.
When the return expression is a reference to an object, a copy of that reference is
returned, not a copy of the object
return keyword
used to return the flow
of control to the point immediately following
where the method or constructor was called.
When an actual parameter is a reference to an
object, the formal parameter is
initialized with a copy of that reference, not a copy of the object.
If the reference is to a mutable object,
the method or constructor can use this reference
to alter the state of the object.
Passing a reference parameter results in the
formal parameter and the actual parameter
being aliases. They both refer to the same object