● Object – characterized by state, attributes, and behavior.
o Instance of a class
● All OOP (Object-Oriented Programming) languages try to represent an object as a variable or an instance in a program.
● Class – Defines another abstract data type in the program
● Object references
o String variables
● Instance Variables
o Attributes and behaviors
o Hold data for objects
● Methods – Code for behaviors or any actions that apply to the objects.
● Constructors – Used to initialize the instance variables
o When an object is created
● Main Methods – Used to test the class
● Instance variables
o Attributes/behaviors
o Fields
o Properties
● Types of methods: Accessor, constructors, and mutators
● Void return type – Indicates that the method doesn’t return any value or String object.
● Method names are always followed by parentheses.
o Possible parameters should be indicated here.
● Example of a class:
public class Name {
private String first;
private String last;
public Name(String theFirst, String theLast) {
first = theFirst;
last = theLast;
}
}
● Keywords public and private are used for:
o Classes
o Data
o Constructors
o Methods
● Constructors – used to set the initial value for an object
o Or instance variables
● When there is no constructor coded, Java includes a default constructor
● Default constructors don’t have any arguments.
o Instance variables are set to a default value.
▪ Int and double – 0
▪ Strings – null
● Constructor parameters are known as local variables to the constructor.
o They also provide data to initialize instance variables.
● Classes often have multiple constructors.
o A constructor which has no parameters
o A constructor with parameters needed for initializing any instance variables.
● // - single line comment
● /* */ - multiple line comment
● /** */ - documentation comment
● These comments help you remember any changes or additions to the program.
o It is a good habit to develop for programmers.
● You can use the Java tool known as Javadoc for this.
● Comments are ignored by the Java compiler.
● Preconditions
o Condition must be true before the code is implemented.
● Postconditions
o Should be true after a method is run
o Describes the output/outcome after the method is run
▪ Can show any changes occurred to the instance variables
● These conditions help determine the validity of the program/software.
● Software designers and programmers usually use this:
o Use-case diagram system
o Shows different ways a user can interact or use the program before its built
● Example with both types of conditions
/**
* Constructor that takes the x and y position for the snake
* Preconditions: parameters x and y are coordinates from 0 to
* the width and height of the world.
* Postconditions: the snake is placed in (x,y) coordinates
* @param x the x position to place the snake
* @param y the y position to place the snake
*/
public Snake(int x, int y)
{
xPos = x;
yPos = y;
}
● Also known as get methods or getters
o How to get the value of an instance variable
● Return by value
o Original value can’t be modified
o A way to access the instance variables of the class
● Non-void method returns only a single value.
o The header has the return type instead of the keyword void
● Accessor methods return primitive types
● Return keyword/ expression
o References an object
o Returns a copy of the reference
▪ Not the original object
● toString method
o Overridden method
o Incorporated in classes to show a description of the object
o Called when print statements are passed as objects
● Also known as a set methods or setters
o Allows changes to the values of instance variables
● Void methods don’t return a value but they do take parameters for instance variables
● Procedural Abstraction: Can name a method and call it whenever it’s needed
o Creating methods
● Programmers break down larger programs into a smaller one.
● To write methods, you need a
o Method definition
o Method Signature
o Method body
● Object.method()
o To call an object’s method
● Actual parameters can be a primitive value or a reference to the object.
● Why should you use multiple methods in your code?
o Organization and reducing complexity of the code
o Reusing code
o Maintainability and debugging
● Static variables and methods use the keyword static.
o Must be before header or declaration
o Can be public or private
o Belong to class
o Objects share single static variables
o Used with class name and dot operator
o Associated with class
o Cannot access or modify any values of the instance variables
o Can modify the values of static variables.
o Can’t call non-static methods
● Scope of a variable – where a variable can be accessed or used
o Determined by the declaration of the variable
● Java has 3 distinct levels of scope which are related to different types of variables
o Class level scope
▪ Instance variables
o Method Level Scope
▪ Local variables
▪ Parameter variables
o Block level scope
▪ Loop variables
● Formal parameters or variables should only be used within a constructor or method.