Java Classes Objects and Arrays for AP CS
Java Classes and Objects
Blueprints for Your Code
In programming, we often want to model things from the real world. Think about a dog. A dog isn't just one thing; it's a concept. There are many individual dogs, but they all share common traits, like having a breed, an age, and a color.
In Java, we use a class to act as a blueprint or a template for these concepts. A class defines what all dogs will have in common, but it isn't a dog itself. It's just the plan for making dogs.
A class is a blueprint from which individual objects are created.
Defining a class is simple. You use the class keyword followed by the name you want to give it. Let's create a blueprint for a Dog.
class Dog {
// The details of the blueprint go here.
}
This code doesn't create a dog. It just defines what a dog is. Right now, our blueprint is empty. Let's add some properties.
Properties and Actions
Our Dog blueprint needs to define the characteristics each dog will have. These characteristics are stored in variables called instance variables. Each dog we create, or each instance of the Dog class, will get its own set of these variables. Let's say every dog has a breed, an age, and a color.
class Dog {
String breed;
int age;
String color;
}
Besides properties, objects can also perform actions. These actions are defined as methods inside the class. A method is a block of code that runs when it's called. Let's give our Dog class the ability to bark and sleep.
class Dog {
String breed;
int age;
String color;
// Method to make the dog bark
void bark() {
System.out.println("Woof!");
}
// Method to describe the dog sleeping
void sleep() {
System.out.println("Zzz...");
}
}
Now our Dog blueprint is more complete. It specifies that any dog made from it will have a breed, age, and color, and will be able to bark and sleep.
Creating Your First Object
With our Dog class blueprint ready, we can now create actual dog objects. An object is a specific instance of a class. To bring an object to life, we need a special method called a constructor.
A constructor's job is to create a new object and set its initial state. If you don't write one, Java provides a default, invisible constructor for you. But it's better to define our own so we can set the dog's properties right when it's created.
A constructor has the same name as the class and has no return type. It initializes a new object.
Let's add a constructor to our Dog class. It will take the breed, age, and color as input and assign them to the instance variables. The this keyword is used to refer to the current object being created.
class Dog {
String breed;
int age;
String color;
// Constructor
public Dog(String dogBreed, int dogAge, String dogColor) {
this.breed = dogBreed;
this.age = dogAge;
this.color = dogColor;
}
void bark() {
System.out.println("Woof!");
}
void sleep() {
System.out.println("Zzz...");
}
}
Now we have everything we need to create, or instantiate, a Dog object. We use the new keyword to call the constructor. Let's create two different dogs.
// In another class, like one with a main method
public class Kennel {
public static void main(String[] args) {
// Create a Dog object named myDog
Dog myDog = new Dog("Bulldog", 5, "White");
// Create another Dog object named buddy
Dog buddy = new Dog("Golden Retriever", 2, "Golden");
// Call the bark() method on the myDog object
myDog.bark(); // Output: Woof!
// Print buddy's age
System.out.println(buddy.age); // Output: 2
}
}
Here, myDog and buddy are two separate objects. They were both made from the Dog class blueprint, but they have their own independent sets of instance variables (breed, age, color). Changing one dog's age won't affect the other's. This separation is the core idea of object-oriented programming: creating self-contained objects that bundle data and behavior together.
What is the primary role of a class in object-oriented programming?
In the context of Java, an object is a specific __________ of a class.