AP Computer Science A
Advanced Placement Computer Science A focusing on Java programming and object-oriented design.
Classes and Objects
Introduction to Object-Oriented Programming
Java is an object-oriented language, which means it organizes code around objects and classes.
What is a Class?
A class is a blueprint for an object. It describes what an object knows (its variables) and what it can do (its methods).
public class Dog {
String name;
int age;
void bark() {
System.out.println("Woof!");
}
}
What is an Object?
An object is an actual thing created from a class. You can make many objects from the same class.
Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.age = 4;
myDog.bark();
Why Use Classes and Objects?
They allow you to organize code, reuse logic, and create complex programs that mirror real-world things.
Real-World Analogy
A class is like a recipe, and objects are cakes you bake from that recipe!
Examples
Defining a class called
Car
with variables formake
andmodel
.Creating two
Student
objects namedalice
andbob
from aStudent
class.
In a Nutshell
Classes are blueprints; objects are the things you build from them.