D.1.2 Distinguish between an object (definition, template or class) and instantiation.
A class is the blueprint from which individual objects are created
A class is a description of a group of objects with similar properties and behaviours
A class is a data type that defines variables for the state of an object and methods for an object's behaviour
A "Rectangle" has length and width. A "Rectangle" can change the values of its length and width, calculates its perimeter and area, and displays these information in a text (String) form.
To translate the above description of the object "Rectangle" into a java program code (Java class):
length and width of the "Rectangle" are its fields and will become the Java class's member variables
its ability to set values for its length and width, calculate its area and perimeter and displays its information will become the Java class's methods.
you may add some more functions/behaviour to what have already been identified as methods from the above description
/** * * @author maanderson */
public class Rectangle {
//member variables
private int length, width;
//constructor with two arguments
public Rectangle(int l, int w){
length = l;
width = w;
}
//a no-argument constructor with member variables assigned with initial values
public Rectangle(){
length = 2;
width = 4;
}
//changes or sets the value of length
public void setLength(int length) {
this.length = length;
}
//returns the length
public int getLength() {
return length;
}
//changes or sets the value of width
public void setWidth(int width) {
this.width = width;
}
//returns the width
public int getWidth() {
return width;
}
//calculates the area
public int getArea() {
int a = getLength() * getWidth(); return a;
}
//calculated the perimeter
public int getPerimeter() {
int p = 2 * (getLength() + getWidth()); return p;
}
//returns the information of the rectangle in text (String) format
@Override
public String toString() {
String s = "rectangle length: " + length + "\n rectangle width: " + width; return s;
}
}
Objects are created out of Classes. Many distinct objects (instances of a class) can be created from a single Class.
Rectangle rectangle1; // declaring a reference variable, its value will be undetermined
// until an object is actually created and assigned to it.
Rectangle rectangle2 = new Rectangle(100, 200); // creates an object of the Rectangle class.
Rectangle rectangle3 = new Rectangle(50, 100); // creates another object of the Rectangle class.
Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes a new object.
Constructors are methods, which are called during the instantiation of an object with the new operator.
The primary purpose of a constructor is to initialize all the attributes of newly created object.
Constructors have the same identifier as the class.
Constructors are neither void methods nor return methods.
Constructors can be overloaded methods. That is, the method identifier can be the same, but the method signature must be different, to allow for different possibilities when constructing an object.
How many instances of a class (Rectangle) are created in the above code? (Refer to Code Example #02)
Which constructor/s were used to create the last two Rectangle objects? (Refer to Code Exam #01)
What do the numbers in the last two Rectangle objects represent? (Refer to Code Example #02)
What is the difference between rectangle1 and rectangle2 variables? (Refer to Code Example #02)