Please enable JavaScript.
Coggle requires JavaScript to display documents.
More on Objects and Classes (constructor (The name of a constructor is…
More on Objects and Classes
public interface
Every class has a
public interface
the interface is all of the methods in the obj class
Encapsulation
when you have a public interface but you hide implementation details
Why have it?
you can change details of implementation without affecting users
When will you use encapsulation?
When you design your own classes you will use it, bc you
specify a set of public meths and hide implementation details
you need to use private vars in order to encapsulate
Behavior and State of an Obj
behavior
defined by the meths in an obj
instance meths = those used on objs
state
= internal data
its state is stored in instance vars
an instance var is present for EACH obj
instance var set up
modifier - public, private, etc
private modifier means that the var can only be accessed by meths in the same class
this leads to needing a getter meth
type - int, double, etc
name of var
Shared References
You can have two (or more) object variables that store references to the same object, for example by assigning one to the other. CashRegister reg2 = reg1;
Mutator vs Accessor Meths
Mutator
modifies the object
Accessor (Getter)
queries the object for some information without changing
it
Static vs Instance Meths
static
decl using keyword static
not invoked on an obj
instance
meths in obj class
invoked on an obj
constructor
initializes instance vars of an obj
automatically called whenever an object is created with the new operator
The name of a constructor is identical to the name of its class
public class CashRegister{...}
Constructors never return values, but you do not use the void reserved word when
declaring them
You can construct more than one constructor. The one that matches param awise when called will be the one that is used
If you do not initialize an instance var in a constructor, it is automatically set to a
default val
nums = 0, boolean = false, objs = null
If you do not write a constructor for a class -> default constructor -> has no arguments, and it initializes all instance
variables with their default values.
tips
—most classes don’t contain a main method. They are meant to be combined with a class that has a main method
Testing in isolation, outside a
complete program, is called unit testing
you can write a tester class. A tester class is a class with a main method
that contains statements to run methods of another class.
the null reference
if an obj DNE then you cann assign null to it
ex: String str = null;
VERY diff from String str = ""; bc an empty string has length 0 and a null one DNE
you can use == or != to test if it's null
this is the default reference when another obj is being referenced in an obj or for an obj reference in an array of objs
static vars & meths
static vars
belong to the CLASS not a METHOD (it is declared before any meth - including the main meth)
ex: private static int lastAssignedNumber = 1000;
should always be decl private (instance vars too)
there is only one copy of these, whereas for instance vars - vars connected to objs - there is a copy of the var for EVERY obj
static meths
There aren't many static meths in obj-oriented programming
there has to be a main static meth bc when the program starts there are NO objects
packages
collection of similar classes
can be called with import statement and a declaration of the class you want to use. For ex, scanner
to import all classes from a package just put
at the end. Ex: java.util.
; - NOTE - all classes from the lang class are automatically imported