1/17
Adv. Comp Sci, Lesson 2.3 - Creating and Storing Objects
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
OOP
A programming style where users declare classes, create objects from classes, and write programs with interacting objects.
Object
An instance that can be used in code as an instance variable (state) and a method (behavior).
Class
A template or blueprint from which an object can be created, defining the object's data and methods.
Instance Variable
A variable that defines what data objects will use; they are private and declared without initial values.
public class Dog {
private int age;
private String name;
private boolean goodDog;
}Constructor
A special method that makes an object public and saves memory to give instance variables values; it does not return values.
int age = 0;
String name = "unknown";
boolean goodDog = false; Default Constructor
A constructor with no parameters that initializes instance variables to default values.
int age = 0;
String name = "unknown";
boolean goodDog = false; Parameter Constructor
A constructor that uses the memory saved from the default constructor to add values to the instance variables; can have unlimited uses throughout the class.
public class Dog
{
//instance variables here
//default constructor here
public Dog(String n, int a, boolean g)
{
this.name = n;
this.age = a;
this.goodDog = g;
}
}
Creating Behaviors"this" Keyword
Used in parameter constructors to differentiate between instance variables and parameters with the same name.
this.name = n;
this.age = a;
this.goodDog = g;
Methods
Behaviors that objects can have, which can be public or private depending on their accessibility in classes.
Why are instance variables private?
Back: Instance variables are kept private to encapsulate the data, meaning methods outside of the main class cannot access it.
What are instance variables not given when they are declared?
When they are declared, their variables are not assigned values.
Why are constructor variables given default values?
Ensures that the objects are at a known state to prevent errors from uninitialized variables.
What do parameter constructors list when giving values to variables?
the variable identifier and the data type.
reference memory
memory used to store data and objects in a program. It refers to certain instance variables.
What are reference variables used for?
To refer to methods in the object creating class.
void type
a method that does not return data.
relationship between parameters and methods
Parameters are inputs that methods use to perform actions and calculations. Sometimes they can modify variables.