Article image Classes and Objects: Instantiating Objects

17.3. Classes and Objects: Instantiating Objects

Page 47 | Listen in audio

In object-oriented programming, classes and objects are fundamental concepts. A class is a structure that defines the states and behaviors that an object can have. On the other hand, an object is an instance of a class, which has states and behaviors defined by the class. In this section, we will discuss how to instantiate objects in programming logic.

Instantiating Objects

Instancing an object is the process of creating an object of a class. In object-oriented programming languages, this is done using the 'new' operator. When an object is instantiated, memory is allocated to hold the object and its attributes are initialized to their default values.

For example, in Java, you can create a class 'Car' and instantiate an object of that class as follows:

class Car {
    String color;
    String tag;
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
    }
}

In this example, 'myCar' is an instance of the 'Car' class. The 'new' operator is used to create the instance and memory is allocated to store the 'myCar' object.

Object Attributes

The attributes of an object are the variables that store the state of the object. In the example above, 'color' and 'make' are attributes of the 'Car' class. When an object is instantiated, its attributes are initialized to their default values. In Java, default values ​​are null for object references, 0 for numeric types, and false for Booleans.

An object's attributes can be accessed and modified using dot notation. For example, you can set the color and branding of 'myCar' as follows:

myCar.color = "Red";
myCar.brand = "Ferrari";

Object Methods

An object's methods are the functions that define the object's behavior. They can access and modify object attributes and perform other operations. For example, you can add a 'accelerate' method to the 'Car' class as follows:

class Car {
    String color;
    String tag;

    void accelerate() {
        System.out.println("The car is accelerating.");
    }
}

You can call the 'accelerate' method on 'myCar' as follows:

myCar.accelerate();

This will print "The car is accelerating." on the console.

Constructors

A constructor is a special method that is called when an object is instantiated. It is used to initialize object attributes. In Java, the constructor has the same name as the class and has no return type. For example, you can add a constructor to the 'Car' class as follows:

class Car {
    String color;
    String tag;

    Car(String color, String brand) {
        this.color = color;
        this.mark = mark;
    }

    void accelerate() {
        System.out.println("The car is accelerating.");
    }
}

In this example, the 'Car' constructor accepts two parameters, 'color' and 'make', and uses them to initialize the object's attributes. You can instantiate a 'myCar' with a specific color and make as follows:

Car myCar = new Car("Red", "Ferrari");

In summary, object instance is a fundamental concept in object-oriented programming. A class defines the states and behaviors that an object can have, and an object is an instance of a class. Instancing an object involves allocating memory for the object and initializing its attributes to their default values.

Now answer the exercise about the content:

What is the purpose of a constructor in object-oriented programming?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Classes and Objects: Constructors

Next page of the Free Ebook:

48Classes and Objects: Constructors

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text