Introduction to OOP — Object, Constructor, Default Constructor
In this chapter, we will learn about objects, constructors, and default constructors.
In the last chapter, we defined the getter methods for our Toy class and our implementation looked like this:
public class Toy {
private int ID;
private int price;
private int quantity;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}Object
An object is an instance of a class. A class is called a blueprint of an object. We can compare this to real-world objects—for example, the design of a car is the blueprint and when the vehicle comes into existence it becomes an object.
To create an object of a class we write:
Toy car = new Toy();In the above statement, the Toy class is the blueprint and the object is the instance created in the HEAP by the statement new Toy().
In the real world, an object (like a human, animal, etc) has properties. For example, a human has hands, eyes, legs, etc. Similarly, in Java an object that exists in the HEAP (real word for Java) has properties. An object of the Toy class has three properties: ID, price, and quantity.
Constructor
Now let's create an instance or an object of the class Toy and use setter methods to initialize it with the proper value:
Toy car = new Toy();
car.setID(10);
car.setPrice(100);
car.setQuantity(3000);The object looks like the following:
Now let us try to remove the setter methods from the Toy class:
public class Toy {
private int ID;
private int price;
private int quantity;
public int getID() {
return ID;
}
public int getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
}We cannot write the following anymore, it will give a compile error:
Toy car = new Toy();
car.setID(10);
car.setPrice(100);
car.setQuantity(3000);The last 3 statements will give a compile-time error. If we remove those 3 statements then thing looks like the following:
Toy car = new Toy();The object created has properties ID, price, and quantity set to 0. Java provides a default value for the properties when an object is created based on the type of the properties. Here, all the properties are of integer type, so Java provides 0 as the default value for all the properties.
The object looks like this:
Now, how do we create an object that has ID set to 10, price set to 100, and quantity set to 3000? The answer to the question is that we need to use a constructor. So, what is a constructor?
Get rakibul rushel’s stories in your inbox
Join Medium for free to get updates from this writer.
A Constructor is a method with the same name as the class that does not have any return type and is invoked when you create an instance or object from the class. Here is the implementation:
public class Toy {
private int ID;
private int price;
private int quantity;
public Toy(int ID, int price, int quantity) {
this.ID = ID;
this.price = price;
this.quantity = quantity;
}
public int getID() {
return ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}Now if we want to create an object from the class we need to write:
Toy car = new Toy(10, 100, 3000);If you look at the above statement we used the keyword new followed by the constructor Toy(10, 100, 3000). The statement:
new Toy(10, 100, 3000)will invoke the constructor (just like methods). We have passed 10, 100, and 3000 as arguments of the constructor. Now, our car instance looks like the following:
So, we can use the constructor to initialize an object. We initialized the car object with an ID equal to 10, price equal to 100, and quantity equal to 3000.
Now let's learn about the default constructor.
Default Constructor
You have seen previously we wrote the following to create an instance:
Toy car = new Toy();But there is no constructor defined in the Toy class which has no parameters. Here is our Toy class which does not contain any constructor.
public class Toy {
private int ID;
private int price;
private int quantity;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}So, where did the constructor without any parameters come from? the answer is the parameterless constructor is provided by the Java compiler.
The parameterless constructor is called the default constructor. You can also provide the default constructor:
public class Toy {
private int ID;
private int price;
private int quantity;
public Toy() {
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}Whenever you provide your constructor the compiler will not provide the default constructor. Here we are providing our own constructor:
public class Toy {
private int ID;
private int price;
private int quantity;
public Toy(int ID, int price, int quantity) {
this.ID = ID;
this.price = price;
this.quantity = quantity;
}
public int getID() {
return ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}If you now try to do the following:
Toy car = new Toy();The compiler will throw a compile-time error because you provide a constructor. The compiler cannot find a constructor without parameters.
To solve the problem we can also include a default constructor:
public class Toy {
private int ID;
private int price;
private int quantity;
public Toy() {
}
public Toy(int ID, int price, int quantity) {
this.ID = ID;
this.price = price;
this.quantity = quantity;
}
public int getID() {
return ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}Now the following code will work:
Toy car = new Toy();But, why do we need a default constructor? Suppose every toy in our toy store has the price set to 100 dollars initially. All of the other properties will be initialized with 0. Here is the implementation:
public class Toy {
private int ID;
private int price;
private int quantity;
public Toy() {
ID = 0;
price = 100;
quantity = 0;
}
public Toy(int ID, int price, int quantity) {
this.ID = ID;
this.price = price;
this.quantity = quantity;
}
public int getID() {
return ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}Now let's create an object of class Toy using the default constructor:
Toy car = new Toy();The object will look like the following:
Later we can change the properties of the object using the setter methods.
So, the default constructor can be used to initialize the properties of an object with the values we want.
That is it for this chapter. The next chapter will be coming soon.







