Session 3 |
Objects
Let us now look deep into what objects are:
Real-world objects
We can find many objects around us: cars, dogs, humans, etc. All these objects have a state and a behavior. If we consider the dog (an object), then its state can be: 3-year-old brown labrador Liza, and the behavior is - barking, wagging the tail, running.
Software object
A software object is an instance of so-called object class created in the client class. A software object is very similar to a real-life object: a software object stores its state in the instance fields and exposes its behavior through methods.
Simple object class and client class
In this example, we are going to create a simple object class Dog.
- Object class named Dog would be a prototype or blueprint for all dogs (instances) in our program. It would describe dogs in general without specific values neither behavior methods execution.
- Object class named Dog would also be our brand new data type.
- Inside the object class, we are going to declare a few variables which represent properties common to all dogs (e.g. color, age, size, breed). Variables defined within a class are called object or instance variables, fields or attributes.
// create a java file called Dog.java public class Dog { // Instance fields are declared right after the class header String name; String breed; int age; String size; String color; }
To use the brand new data type Dog, we need another class - client class - with the main method:
// create a new java file called Execute.java public class Execute{ public static void main(String[] args){ Dog liza = new Dog(); } }
Now we have created an instance of object class Dog. However, this instance liza does not represent our labrador Liza yet because we have not given values to variables name, breed, age, size, color. In fact, at the moment these variables contain the default values such as null or 0.
Self-assessment
Add the following line of code System.out.println(liza.breed);
into the main methods.
What is the result?
There are several ways to set values to the variables.
Option 1. Change the object definitions in the object class:
// update java file called Dog.java public class Dog { // Instance fields String name = "Liza"; String breed = "labrador"; int age; String size; String color; }
Option 2. Set variables in the client class:
// update java file called Execute.java public class Execute{ public static void main(String[] args){ Dog liza = new Dog(); liza.name = "Liza"; liza.breed = "labrador"; // Once the field variable is set, // we can refer to it by object_name.instance_field (so called dot notation) System.out.println(liza.name + " is a " + liza.breed); } }
Option 3. To call a more powerful constructor than the default one new Dog()
. This option will be explained in detail on next page.
Is it a class or an object or an instance?
The distinction between the terms class definition and the objects is easy to misunderstand. The terms are very similar and are sometimes interchangeable. In fact, an instance refers to a specific object, which is created from an object class.
Object class (= Class definition or Object definition)
- specifies the type of an object;
- specifies types of "attributes" that all objects of this type will have;
- specifies "behaviors" of its objects in methods that all objects of this type will have;
- does not have the main method;
Java program can contain multiple object definitions (= object classes).
Object (= Instance)
- instances are created when the program runs;
- instances can be created using operator
new
; - each instance holds its individual properties values in the instance variables (instance fields);
- each instance executes behavior on its personal set of properties values.
Creating an object of an object class is called instantiation.
Session 3 |