Home OOP Option Last Next


Distinguish between an object (definition, template or class) and instantiation.

JSR Note: This is better stated:
Distinguish between the template class definition, and the instantiation of objects of that class.


Teaching Note:

Students must understand the difference in terms of code definitions, memory use and the potential creation of multiple instantiated objects.


Sample Question:


Q1. A large company with locations in different cities has taken an OOP approach in creating an
administration program that manages all aspects of its business. These aspects include:
- the sale of all of the different products that the company manages
- the salaries for managers, office staff and sales personnel.

By making use of an example from the above scenario, distinguish between a class
and an instantiation of a class.




JSR Notes:


A Rose is a Rose is a Rose, sure.

But is an Object and Object, an Object??

Object is one of those terms that can be defined and used several different ways, and a couple of those ways are tricky to explain to begin with. So you have to consider context when answering "what is an object?". Fully understanding all of this will require a bit more time and spiralling, but for now, consider the following, all slightly differently phrased, questions:

A. What are objects? (as in, generally speaking)

B. What is an object? (focusing in on "an")

C. What, literally, is an object? (so here we would be thinking in terms of memory)

D. What is a Java object? (i.e. the Java context)


A. What are objects? (as in generally speaking)
Objects are user defined types - i.e. non-primitive types - made by the programmer to represent various classes of things they are working with.

B. What is an object? (focusing in on "an")
An object is a particular instance of a user defined class.

C. What, literally, is an object? (so here we would be thinking in terms of memory)
An object, literally, at the memory level of computers, is an address. That address points to the data associated with the object.

D. What is a Java object?
A Java object is an instance of a user-defined class, which, to one level of hierarchy or another, is ultimately extended from the "mother of all Java classes", the Object class.


Theory of Template Class vs. Object Instance

Template Class

A "template" class is the class which exists as model for making instances of that particular type. Keep in mind that it actually is a real, coded class. But you can think of it as being abstract because it itself is not used as a particular object- rather it is used as the model to make usable instances/objects.

Another way to think of a template class is as being a cookie cutter. And the instances/objects made from its model are the usable (eatable!) cookies. The cookie cutter is real, but is is not the object that is actually used when it's time to have a sweet snack!

Objects & the Instantiation of Objects

An object is one particular instance of a certain template class. In terms of physically how it exists in the running computer, the object is actually a memory location holding the address of where the attributes and methods of that instance are stored in RAM. Just like a primitive variable, for ease of working with it, rather than trying to remember and keep track of that particular memory address, an object is represented by a certain variable identifier we give it when we "instantiate" it. Below are a few instantiations of a particular object of the Student class, which we have chosen to give it the identifiers student1, student2, and student3.

Student student1 = new Student("Sally", 18);
Student student2 = new Student("Bob", 17);
Student student3 = new Student("Kelly", 17);

Having executed theses line, there would exist not only the Student template class - previously programmed - but three particular realizations of the abstract idea of that Student class. So basically copies of the Student class, but with a particular states - Sally - 18, Bob - 17, and Kelly - 17.

Having been instantiated, objects are conceptually similar to real-world objects: not only do they consist of a particular state, but they can do all of the behaviors that have been coded in the template class to work with the those attributes. Given the two particular attributes of this class, some likely behaviors would be things such as getting the name and age, and re-setting the name and age.



Actual Java Implementation

Template Class Definition

First you make the template class (and then you make instances of it in another class). So my Student "template" class might be defined as:

public class Student{
    private String name = "not set yet";
    private int age = -999;

    public Student(String name, int grade){
        this.name = name;
        this.age = age;


The above code is the actual class definition of the Student class. Or put another way, we can say it is the Student "template class".


Making an Instance/Object of a Template Class

After having made a template class, you then use the template class by making instances of it. Or put another way, you make objects of that class. This could be just one, or a handful of instances/objects, but usually you end up making many instances/objects of a certain class.

So from a "main" etc. class, you would see "instantiation" statements (with the new operator):

Student student1 = new Student("Sally", 18);
Student student2 = new Student("Bob", 17);
Student student3 = new Student("Kelly", 17);

The above are three actual instances or objects of the template class Student.

And each line above is indeed instantiation of a unique object of the Student class. Instantiation, then, is making a new instance of an object by creating new memory for an address which points to the object, and calls its constructor.

Note that we have been using instances of template classes almost from day one. Here are a couple of examples that should ring a bell:

Scanner snr = new Scanner(System.in);
String s = new String("Hello");

Above, snr is an instance of the Scanner class, and s is an instance of the String class (though String is a special case and it can be treated as a primitive as well - more on that later on in the Option.) There are lots and lots of other "libraries" of template classes we can make use of later on, along with making our own.


Memory Usage of Template Classes and Objects

In terms of the "memory use" referred to in the Teaching Note, the original class definition itself takes up a certain amount of memory based on the number and type of data attributes, and the associated methods. But note that each individual object of the class which is instantiated also takes up exactly that much memory again. i.e. each instance of a class has its own attributes (name and grade in the example above) along with its own methods (the constructor and gets and sets for example).

In making an object of a certain class, the main thing we need to do in terms of memory, is find enough available memory to store that object. All objects will have particular memory requirements based on their attributes. So, in the example below, 80 bits will be required to store the two attributes (16 bits for the char + 64 bits for the double). It is the new operator that is responsible for finding and allotting that new memory. (new is one of the 50 keywords of Java.)

Here's another example of a template class:

1 package simpleOOPExample;
3 public class Team {
5    //Attributes
6     private char teamLetter = 'z';
7     private double averageGoals = -999;
9      //Methods
10     public Team(char teamLetterSent, double averageGoalsSent){
11         teamName = teamNameSent;
12         averageGoals = averageGoalsSent;
13     }
15     //gets... 
17      //sets...
19  }

And making instances of it:

 6 package simpleOOPExample;
 9 public class MainClass {
10     public static void main(String[] args) {
11         Team example = new Team('A',  3.2);
12         Team anotherExample = new Team('B', 1.6);
13         System.out.println("Team" + example.getTeamLetter() + "has an average goals of" + example.getAverageGoals());
14         System.out.println("Team" + anotherExample.getTeamLetter() + "has and average goals of" + anotherExample.getAverageGoal());
15     }
16 }



Back to the Assessment Statement "Distinguish between an object (definition, template or class) and instantiation", here is how you could address it in a sentence:

A class describes/provides the template for a particular object, whereas an instantiation of a class actually creates an object for that class.
For example: A Student class would contain attributes and actions that describe a student, and an instantiation would represent a particular student.