Unit 2: Using Objects
Objects in Java
What are Objects?
Objects are instances of classes and are fundamental building blocks in object-oriented programming (OOP). They can be thought of as blueprints that define properties (attributes) and behaviors (methods) that an entity should have.
Classes: The Blueprint
Classes are Blueprints for Objects
Classes are blueprints for creating objects. They define the attributes and methods that an object should have. For instance, a Dog class can define the attributes breed and age and the method bark(). Then, we can create objects of the Dog class that have these attributes and methods.
A class serves as a blueprint for creating objects. It defines a set of attributes (also known as fields or variables) and methods that can operate on those attributes. For instance:
public class Dog {
// Attributes
String breed;
int age;
// Methods
void bark() {
System.out.println("Woof!");
}
}
In the above example, Dog is a class with two attributes (breed and age) and one method (bark()).
Creating Objects: The new keyword
To create an object in Java, we use the new keyword followed by the class's constructor. For our Dog class:
Dog myDog = new Dog();
Creating an Object
With the line Dog myDog = new Dog();, we're creating a new object of the Dog class and assigning it to the myDog variable. Now, myDog is an instance of the Dog class.
Accessing Attributes and Methods
Once an object is created, we can access its attributes and methods using the dot (.) operator:
myDog.breed = "Golden Retriever";
myDog.age = 5;
myDog.bark(); // Outputs: Woof!
Accessing Attributes and Methods
The dot operator allows us to access and modify the attributes of an object and call its methods. For instance, myDog.breed
accesses the breed attribute of the myDog object.
Importance of Objects in OOP
Object-oriented programming (OOP) revolves around the concept of objects. The main advantages of OOP include:
- Modularity: Code is organized into classes and objects, making it easier to manage and maintain.
- Reuse: Classes can be reused in different parts of a program or in other programs.
- Encapsulation: Classes encapsulate data (attributes) and the methods to operate on the data, providing a clear structure.
- Polymorphism: Objects can take on more than one form depending on their context, adding flexibility to the code.
Summary
In this lesson, we delved into the concept of objects in AP CS A. We looked at classes as blueprints for creating objects, the process of object creation using the new keyword, and how to access and manipulate attributes and methods of objects. Understanding objects and their role in OOP is fundamental for mastering AP Computer Science A and for programming in Java in general.
References
- Oracle Java Documentation: Classes
- Oracle Java Documentation: Objects
- Oracle Java Documentation: Classes and Objects
AP CSA Homework Assignment
Assignment
Instructions
Create a new Java class called Car. Add the following attributes to the Car class:
- Create a new Java class called Car.
- Add the following attributes to the Car class:
- make: The make of the car (e.g. Toyota, Honda, etc.)
- model: The model of the car (e.g. Camry, Civic, etc.)
- year: The year the car was manufactured.
- color: The color of the car.
- price: The price of the car.
- Add a method called drive() to the Car class that outputs the following message: "Driving a <year> <make> <model>."
- Create a constructor for the Car class that takes in the make, model, year, color, and price as parameters and assigns them to the corresponding attributes.
Create a new Car object in the main() method of the Car class and assign it to a variable called myCar. Set the attributes of myCar to the following values:
- make: Toyota
- model: Camry
- year: 2015
- color: Red
- price: 15000
Call the drive() method on myCar.
Expand upon this as much as you like. Build other classes and objects. Have fun!