2.1 Objects: Instances of Classes
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 are variables of user-defined data types. You can refer to these as reference type variables. Just as you initialize and declare primitive types, reference types are also declared and initialized. The initialized form of a reference type is referred to as an object.
Objects are structures that contain a state and behavior. Everyday objects you commonly use have states and behaviors. For example, a car is an object with both a state and behavior.
State
The state contains information about the specific object. An example of a state for a car would be how much fuel it has.
Behavior
The behavior is the actions that can be performed on a specific object. A behavior of the car may be to get the mileage from the remaining fuel.
Classes are the templates you use for creating objects.
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:
Here is another example of a class that takes in a vehicle type, its age, and how many miles it has:
It is important to remember that classes are just templates for creating new objects. A class is a formal implementation, or blueprint, of the attributes and behaviors of an object.
An object on the other hand is a specific instance of a class with defined attributes. Objects contain both a state and behavior, and are an instance of a class.
Instances
An instance is a specific version of an object that can differ in numerous ways. Going back to the previous section, Rectangle
, Animal
, and Vehicle
are all classes. If you create two different vehicles they would be specific instances of the Vehicle
class. Or in other words, they would be two different vehicle objects. Remember, an object is an instance of a class.
Examples
Here is an example of the first piece of a class definition. This code will be used later to create objects. Until we use the class definition to instantiate objects, this code will not do anything.
Hence, this code is for you to look at and not to run. Nothing will happen if you hit run.
Here is an example of the first piece of a class definition. This code will be used later to create objects. Until we use the class definition to instantiate objects, this code will not do anything.
Hence, this code is for you to look at and not to run. Nothing will happen if you hit run.
Here is an example of the first piece of a class definition. This code will be used later to create objects. Until we use the class definition to instantiate objects, this code will not do anything.
Hence, this code is for you to look at and not to run. Nothing will happen if you hit run.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Consider this class definition of
Weather
.When we use this class to create
Weather
objects, which of the following is guaranteed to be true?
This class represents IceCream
.
Add the instance variables, or attributes, that every ice cream object should have to the IceCream.java file. Remember to give the variables a type and a name. They should not have values at this point.