Classes
A class is a blueprint of an object. Classes can be used to define attributes and behavior of our object/s
Attributes
- tells us different characteristics about our object
- is represented by variables within the class
Behavior
- provides possible actions that can performed by the object
- represented by methods in a program
Class
public class class_name{
// class code
}public - modifier, modifier that makes the object publicly available
class - key keyword, keyword for defining a certain object
class_name - class name, a string stating the base name of the object(class)
pair of braces - symbols for enclosing components.
Field
Note: field and attributes are the same thing
private String name;private - modifier, defines the availability of field
String - data type
name - name of the field
Method
Can be interpreted as a function
public void setName(String name){
// method code
}- Method Signature
public- modifiervoid- return typename- identifier for the method- pair of parenthesis - encloses the parameter list
String name- parameter list- to add variables to the parameter list, initialize the variables like normal and separate each variable with a comma
,
- to add variables to the parameter list, initialize the variables like normal and separate each variable with a comma
- Method Body - code within the curly braces
Note
Methods can be overloaded, this is done by defining 2 methods with the same name but different parameter list.
Example
public void printMax(int num1, int num2){ /* ... */ } public void printMax(double num1, double num2){ /* ... */ }The difference in the parameter list can be:
- number of parameters
- type of parameters
- order of parameters