Object-Oriented Programming
What is OOP?

The course is part of this learning path

Start course
1h 27m

In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.

Learning Objectives

  • Object-oriented programming in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there, my friend. In this video, we will talk about the constructor in Java. Constructor is similar to a method. It has the same name as its class. When a new object is created at least one constructor will be invoked. Every class has a constructor that is named the default constructor. However, constructors have no explicit return type. As you see in our example, a class can have more than one constructor. Now, let's look at an example to better understand how constructors work. Let's say we have a class car, when we create two objects of car, in creating the first object, the new keyword here creates the object of class car and invokes the default constructor that has no parameter to initialize the newly created object. In creating the second object, the new keyword also creates the object of class car and invokes the constructor, which has one parameter to initialize the newly created object. Now, let's create a project and use constructors. 

First, let's create a new Java project and specify the project name as Object Oriented. Don't change the other options and click the finish button. In this project, right click on the source folder and select new class, specify the package name as oopconcept and the class name can be Car. In this instance, I will not check the checkbox for inserting a main method into our code. I will only create a Java class, so I'll click the 'finish' button. Now we have a car class. The states of the car are model and current speed. We declare them with a private access modifier because I don't want to access these variables from other classes, so I write private String model, private int currentSpeed. Now let's declare a public constructor named Car and it takes one parameter and its name is model. We declare the constructor as public as we would like to access this constructor from other classes. Constructors are usually public because their purpose is to create objects of the class from different classes. Also notice that the constructor has no return value. In the constructor method, I will assign the model parameter here to the own variable of this class i.e. model. 

To do this, we use the this keyword in the constructor. Don't worry, we will learn about the 'this' keyword very soon. So, let's just continue for now. Here I write this.model = model. So, the this keyword represents the car class and the model here represents the model variable in this class. The purpose of this parameter is, that when we want to create an object from the car class we will need to write a value corresponding to the parameter we want here. Just like when we call a method with the same parameter in a different place we give its parameter. In other words, thanks to this parameter, we will be able to assign a value to the model variable of this class from a different class. You can also write a different name here. For example, we could call it carModel. In most cases however, the parameter name is the same as the variable name. So, let's continue. Let's declare four public methods of the car class. We declare it as public because we want to access these methods from other classes. The name of the first method is start with void type. In the start method, we will use the print method to display the 'car has started' message by adding the variable model. So, I write S.out this.model + " has started ". The name of the second method is accelerate. We will add 20 miles per hour to the current speed variable by using the add an assignment operator. So, I write this.currentSpeed += 20. The name of the third method is stop. In the stop method, we will use the print method to display the 'car has stopped' message by adding the variable model. So I write

S.out this.model + " has stopped ". Also, I will set the current speed to zero here because if the car stops, then its speed will be zero. So, I write this.currentSpeed = 0. The name of the last method is show speed. In the show speed method, we use the print method to display the current speed of the car by adding the variable's model and current speed. So I write S.out "The current speed of ". After the plus sign I write, this.model, and plus sign, again, inside the double quotes I write is and lastly this.currentSpeed. So, the console message should return the current speed of Ferrari is 20. Let's save the code. We need to test this class. Let's create a new class. Right click on the oopconcept package and select new class, specify the class name as car test and select the checkbox for the main method. Let's create an object of the car class by using the new keyword, Car myCar = new Car. We pass Ferrari as a parameter to the car class. This process invokes the constructor that has one argument. Let me explain this briefly. The new keyword here actually invokes the constructor method in the car class. Note that in the constructor method there is a string type parameter. 

Therefore, we need to assign a value corresponding to this parameter in the object creation process. So, we wrote Ferrari. Then this Ferrari object will be transferred to the car model variable in the constructor method. If you pay attention, the variable car model will also be transferred to the model variable of this class. In other words, as a result, the Ferrari is transferred to the model variable in the car class with the help of the constructor. Now let's call the methods we defined in the car class by using the myCar object. We can access the properties of an object with a simple dot notation. If we write myCar, Eclipse automatically brings the properties of class and we select them to add to our code. We add the start method first, then accelerate, then show speed and then stop, and again show speed. 

Okay, let's run code in the test class. You see the results in the console?  Start method is called and it displays the 'Ferrari has started' message. Accelerate method is called and set 20 to the current speed variable. Show speed method is called and it displays the current speed of Ferrari is 20. Stop method is called and it displayed Ferrari has stopped and set zero to the current speed variable. Lastly, show speed method is called again and it displays the current speed of Ferrari is zero. Now, you're ready to learn the two string method, but before we move on to the two string method, I want to show you this. First. let's print the myCar object here to the console with the print Ln method. Now let's run the application once again. As you can see, an ID is printed on the console screen, so let's examine this. Notice first the package name. The package referenced by the myCar object. Then the class name, the class referenced by the myCar object and then an ID. If you remember, we talked about stack and heap in our previous lessons. Here the object my Car is created in the stack and has an ID, but also references the memory reserved for the car class in the heap. 

Now let's move on to the two string method and see how we'll change our code. Let's declare two variables in the car class. The first one is color with string type and the second one is year with int type. Let's declare another constructor named Car and it takes three parameters, model, color and year. In the constructor method, we assigned the model to its own variable model by using the this keyword. Color to own variable color by using the this keyword and year to own variable year by using the this keyword. Now let's declare the two string method of the car class, toString method return string. We return name, color and year variables by using the plus operator in this method, Model: this.model, Color: this.color, Year: this.year. Okay, let's save the code. Let's go to the test class, CarTest and let's create a new object. Its name is myCar1. And as you can see this time, two constructors of the car class are offering us. This is for the one parameter, this is for the three parameters. I will choose this constructor this time. We pass three parameters to the car class. The first parameter will be the model, so I write Ferrari. 

The second parameter will be the color, so I write red. The last parameter will be the year, so I write 2022. Now let's print the myCar1 object to the console and see the difference before and after the two string method. Because this time we will not see any id on the console. If you print any object directly, the Java compiler internally invokes the two string method if it is created, otherwise it prints the objects ID in stack memory. Okay, let's run the test class again. You see the result in the last statement, toString method is called and it prints model is Ferrari, color is red and year is 2022. Let's open the car class. We invoke this constructor into string method of the car class. Yes, this is the constructor in Java. In short, constructors have no return value. They have the same name as the class they are in. When a new object is created, at least one constructor will be invoked and you can create multiple constructors. If you don't create any constructor, Java automatically creates an empty constructor. So, there you have it. Let's take a short break here and I'll see you in the next lesson, my friend.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.