Slot 1 Review
Slot 1 Review
CONTENT
2
OBJECTS: STATE & BEHAVIOR
behaviours
3
Class basics
-
Person
class
4
BASIC CLASS STRUCTURE
5
• Inside class
• Outside of any methods
• Declaration Syntax:
private datatype varName;
7
DATA TYPES
char boolean
8
WRAPPER CLASSES
• A method declares executable code that can be invoked, passing a fixed number
of values as arguments.
• A method is a way of reducing code duplication
10
[modifier] returnType methodName([para list]){
// method body goes here
}
Mutator/Setter Accessor/Getter
12
CLASSES: BASIC CLASS STRUCTURE
Class header {
o Instance variables (who uses these variables?) – the class itself (private)
– outsiders (public)
o Constructors (who uses these constructors?)
– outsiders (public)
o Mutators (who uses these mutators?)
Class
Fields Fields
Methods Methods
14
CLASS AS A BLUEPRINT
• The class describes the data (fields), and the behavior (methods), that are relevant to
the real-world object we want to describe. These are called class members.
• A class member can be a field, or a method, or some other type of dependent element.
• If a field is static, there is only one copy in memory, and this value is associated with
the class, or template, itself.
• If a field is not static, it's called an instance field, and each object may have a different
value stored for this field.
• A static method can't be dependent on any one object's state, so it can't reference any
instance members.
Sample
o In other words, any method that operates on instance fields, needs to be non-static. code
15
PACKAGE
• Classes can be organized into logical groupings, which are called packages.
• You declare a package name in the class using the package statement.
• If you don't declare a package, the class implicitly belongs to the default package.
16
ACCESS MODIFIERS: for CLASS
• A class is said to be a top-level class, if it is defined in the source code file, and not
enclosed in the code block of another class, type, or method.
• A top-level class has only two valid access modifier options: public, or none.
Access Description
keyword
public public means any other class in any package can access this class.
When the modifier is omitted, this has special meaning, called
package access, meaning the class is accessible only to classes in the
same package.
17
ACCESS MODIFIERS: for CLASS MEMBERS
Access
keyword Description
public public means any other class in any package can access this class.
protected allows classes in the same package, and any subclasses in
protected other packages, to have access to the member.
When the modifier is omitted, this has special meaning, called
package access, meaning the member is accessible only to classes in
the same package
private private means that no other class can access this member
Sample code
18
19