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:
Here is another example of a class that lets us create new animal objects:
publicclassAnimal{privateString name;privateboolean isPet;privateint age;publicAnimal(String animalName,boolean isAnimalPet,int animalAge) { name = animalName; isPet = isAnimalPet; age = animalAge; }publicStringgetName() {return name; }publicbooleangetPetStatus() {return isPet; }publicintgetAge() {return age; }publicStringtoString() {String aInfo ="This animal's name is: "+ name +" they are currently a pet: "+ isPet +". The animal is "+ age +" years old.";return aInfo; }}
Vehicle Class
Here is another example of a class that takes in a vehicle type, its age, and how many miles it has:
When creating a new object from a class you will use the following format:
[Class Name] [Object Name] =new [Class Name] (params);// Here are some examples using the classes we created earlierRectangle rect =newRectangle(20,8);Animal pet =newAnimal("Cujo",true,7);Vehicle myTruck =newVehicle("Truck",10,173600.4);
Using Objects
Here are some examples on how to create objects with our previous defined classes, and use them.
// Rectangle ClasspublicclassStateBehavior_Rectangle{// Here we will create a new rectangle, and then print its information.publicvoidrun() {// Create the rectangleRectangle rect =newRectangle(20,8);// This will print "Rectangle with width: 20 and height: 8 and area: 160"System.out.println(rect); }}// Animal ClasspublicclassStateBehavior_Animal{// Here we will create a new animal, and then print its information.publicvoidrun() {// Create the animal and assign its attributesAnimal myPet =newAnimal("Cujo",true,7);// Print out the animal infoSystem.out.println(myPet);// This will print: "This animal's name is: Cujo they are currently a pet: true.// The animal is 5 years old." }}// Vehicle ClasspublicclassStateBehavior_Vehicle{// Here we will create a new vehicle, and then calculate how many miles/year it has.publicvoidrun() {// Create the vehicle and assign its attributesVehicle myTruck =newVehicle("Truck",15,173600.4);// Now we will use the `estimateMilesPerYear()` method to determine how many miles// per year, it has been driven.System.out.println("Your yearly mileage is: "+myTruck.estimateMilesPerYear()); }}