Introduction To Classes and Objects

Classes and Objects are utilized in Java as part of the object-oriented programming model. This model focuses on objects and the data and actions associated with the objects.

Objects

Objects are structures that contain a state and behavior. Every day objects we commonly use have states and behaviors. For example, a car is an object with both a state and a behavior.

State

The state contains the information about the specific object.

Thinking back to the car in this case. An example of a state, with the car, would be how much fuel it has.

Behavior

The behavior is the actions that can be performed on the specific object.

A behavior of the car may be to get the mileage from the remaining fuel.

Classes

Classes are the templates we use for creating objects.

Rectangle Class

Here is an example of a class that lets us create a rectangle, and get its area:

Animal Class

Here is another example of a class that lets us create new animal objects:

Vehicle Class

Here is another example of a class that takes in a vehicle type, its age, and how many miles it has:

Creating Objects

When creating a new object from a class you will use the following format:

Using Objects

Here are some examples on how to create objects with our previous defined classes, and use them.

Last updated

Was this helpful?