Object-Oriented Programming: Classes, Objects, and Instantiation

Fundamentals of Classes and Objects

  • Object Definition:

    • An object is a concrete instance of a class.

    • Represents an individual entity containing specific data values and functional abilities defined by its class.

  • Class Definition:

    • A class serves as a blueprint or factory for creating specific objects.

    • It contains the specifications for:

    • What information (data/attributes) the object will contain.

    • What actions (behaviors/methods) the object can perform.

    • Acts as a template from which multiple individual object instances can be instantiated.

Class Structure: Attributes and Behaviors

  • Attributes (State / Properties):

    • Define the data and specifications that an object holds.

    • Example attributes for a Rectangle class:

    • width: Represents the horizontal measurement of the rectangle.

    • height: Represents the vertical measurement of the rectangle.

    • color: Represents the visual color attribute of the object (e.g., "blue").

  • Behaviors (Actions / Methods):

    • Define the operations and functions that an object can execute.

    • Example behaviors for a Rectangle class:

    • calculateArea(): Calculates and returns the area of the rectangle instance.

    • calculatePerimeter(): Calculates and returns the perimeter of the rectangle instance.

    • getDimensions(): Retrieves the dimension details of the rectangle.

    • getArea(): Getter method to retrieve the area.

    • getWidth(): Accessor method to retrieve the current width.

    • getHeight(): Accessor method to retrieve the current height.

Diagram showing the Rectangle class template with attributes and behaviors mapping to object instantiation

Object Instantiation and Creation Syntax

  • Object Creation Expression:

    • Syntax example: new Rectangle(2, 4, "blue")

    • Parameter mapping:

    • 2: Assigned to width

    • 4: Assigned to height

    • "blue": Assigned to color

    • The expression creates and returns the actual Rectangle object.

  • Complete Declaration and Instantiation Statement:

    • Syntax in Java context: java public static void main(String[] args) { Rectangle rect1 = new Rectangle(4, 7); }     

  • Statement Component Breakdown:

    • Rectangle: Data Type (the class name).

    • rect1: Variable Name (the identifier referencing the object).

    • =: Assignment operator.

    • new: Keyword that is always needed to allocate memory and instantiate an object.

    • Rectangle(4, 7): Call to the class's constructor.

    • (4, 7): Arguments passed into the constructor function to initialize state.

Code breakdown diagram illustrating data type, variable name, instantiation keyword, and constructor call

Memory Allocation and Constructor Mechanics

  • Object Instantiation Definition:

    • The formal process of creating an instance of a class.

    • Memory operations:

    • Allocates dynamic memory for the newly created object.

    • References that object's allocated location in memory via the object reference variable.

  • Constructors:

    • Every class has a constructor method that contains the logic required to create and initialize the actual object.

  • Constructor Signature Breakdown:

    • Example constructor signature: public Rectangle(int rectWidth, int rectHeight)

    • Key elements of a constructor signature:

    • public: Access modifier. Constructor is declared public so it can be called from outside the class to create instances.

    • No Return Type: Constructors explicitly have no return type (not even void). This is because a constructor will always implicitly return a reference to the newly instantiated object (e.g., returning a Rectangle object).

    • Rectangle: Constructor Name, which must be identical to the class name.

    • (int rectWidth, int rectHeight): Input parameters accepted by the constructor to set the initial values of the object's instance variables.

Constructor signature diagram highlighting public visibility, matching class name, lack of return type, and input parameters

Default Values for Object References

  • Uninitialized Object Declaration:

    • Example declaration without instantiation: Rectangle rect1;

  • Default Value Characteristics:

    • What is the default value for an object variable that has been declared but not initialized?

    • Default Value: null

    • Role of null: Serves as a placeholder value indicating that the reference variable does not currently point to any object memory address.