Please enable JavaScript.
Coggle requires JavaScript to display documents.
User-Defined Classes and ADTs - Coggle Diagram
User-Defined Classes and
ADTs
The Method
toString
public value-returning method
takes no parameters
returns address of a string object
output using print, println, printf methods
default definition creates String with name of object's class name followed by hash code of object
Class
a collection of a fixed number of components (members)
members are accessed by name
categories/modifiers:
private
public
protected
General syntax for defining a class:
If a member of a class is:
a named constant
declare it just like any other named constant
a variable
declare it just like any other variable
a method
define just like any other method
it can directly access any member of the class--data members and methods
Constructors
With parameters
Without parameters (default constructor)
Properties
same name as the class
has no return type
one or more (in a class)
each constructors must have different signatures
which constructor executes depends on the type of values passed to the class object when the class object is instantiated
all constructors of a class have the same name
constructors are automatically executed when a class object is instantiated
Copy constructor
executes when an object is instantiated
initialized using an existing object
syntax: public ClassName(ClassName otherObject)
Accessing class members:
referenceVariableName.memberName
Assignment Operator: A Precaution
Consider: myClock = yourClock;
copies the value of the reference variable yourClock into the reference variable myClock. Both variables refer to the same object after execution.
Shallow copying: two or more variables of the same type point to the same object
Deep copying: each reference variable refers to its own object
Inner classes
defined within other classes
can be either a complete class definition or an anonymous inner class definition
used to handle events
Variable Declaration and Object Instantiation
General syntax for using the operator
new
is:
new className() OR new className(argument1, argument2,...argumentN)
Example: myClock = new Clock(); yourClock = new Clock(9, 35, 15);
The Modifier
static
in the method heading, specifies that the method can be invoked by using the name of the class
if used to declare data member, data member is invoked by using the class name
static data members of class exist even when no object of class type instantiated
static variables are initialized to their default values
Finalizers
automatically execute when the class object goes out of scope
have no parameters
one finalizer per class
Accessor and Mutator Methods
Accessor method
only accesses the value(s) of the data member(s)
Mutator method
modifies the value(s) of the data member(s)
Creating packages
use reserved word package
define the class to be public
choose name for package
organize package (create subdirectories)
The reference
refers to instance variables and methods of a class
used to implement cascaded method calls
Abstract Data Type
a data type that specifies the logical properties without the implementation details