Logout

D.1.2

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.

----

sdfsdfsf

 

JSR Notes:

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. You can think of it as "the idea" for a certain object. 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.

You can think of the template class as being the 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 identifier we give it when we "instantiate" it. Below is the instantiation of a particular object of the Student class, in which we have chosen to give it the identifier s.

Student s = new Student("Sally", 18);

Having executed this line, there would exist not only the Student template class - previously programmed - but a particular realization of the abstract idea of that Student class. (So basically a copy of the Student class, but with a particular state - Sally, and 18.)

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, the 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;
    }

    //gets...
   
    //sets...
}

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 s = new Student("Sally", 18);
Student s1 = new Student("George", 16);
Student s2 = new Student("John", 17);
//etc.

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.

 

 

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).

 

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.