In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.
Learning Objectives
- Inheritance 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
Prerequisites
- Basic Java knowledge
Hello there, in this video, we will talk about the super and this keywords in Java. The super keyword is used to access the super class members from the subclass. By simply using the super keyword, we simply can access constructors, fields, and methods of the super class. In our example on the left side of the slide, subclass Car is using the super keyword to access the stop() method of the super class Vehicle. The this keyword is used to access the same class members inside the same subclass. In our example, on the right side of the slide subclass car is using the this keyword to access its own start() method. Also, we can access other members of the class, such as variables and constructs by using a similar way. Let's open Eclipse and go through some examples using the super and this keywords. In the Object Oriented programming project, right click on the 'Source' folder and select New Class. I will specify the package name as superthis and class name as Vehicle. Vehicle class is a super class.
Let's declare three instance variables in the Vehicle class. I will declare two variables named type and model with String type. String model, type, and variable maxSpeed within type and maxSpeed. Let's declare a public constructor named Vehicle. And it will take three parameters: type, model, and maxSpeed. In the constructor method, I will assign parameter variable type to variable type of this class. So, I write this.type = type. The this keyword represents the Vehicle class, and the type is the variables of the Vehicle class. But the type we wrote after the equal sign is the parameter of the constructor. So, its value will come outside of this class when we create an object from the Vehicle class. Okay, so, as a result, the this keyword indicates the class it is in.
So, now let's continue on. Secondly, I will assign the parameter variable model to the variable model of this class, and lastly, we assign the parameter variable maxSpeed to the variable maxSpeed of this class. We use the this keyword to access the same class variables. Let's generate public getter methods of these variables to access from the main method. We will touch on the getter and setter methods in detail in the encapsulation lecture. For now, you can do the same thing with me. In order to generate getter methods, we right click on the empty file and select the Source and Generate Getters and Setters options. I will choose the get methods of all variables and then click on the 'Generate' button. Thus all selected getters methods will be generated automatically, and I will save the code. Okay, let's create a new subclass. I will right click on the 'Superthis' package and select New Class.
Specify the class name as Car and click 'Finish'. Our subclass Car will extend super class Vehicle. This means that subclass Car inherits all methods and variables from the super class Vehicle. But if you notice, this time, we got a compilation error. If you place the mouse pointer onto the Car class, Eclipse shows a quick fix dialogue. It says, "Implicit super constructor Vehicle is undefined for default constructor. Must define an explicit constructor. If you remember, in the inheritance lectures, we said that the subclass cannot inherit the constructor of the super class but the constructor of the super class can be invoked in the subclass. All right, for this, if the super class has a constructor with a parameter, you should add it into the subclass, or you have to create an empty constructor in the super class. The empty constructor is the parameterless constructor. So, we define a constructor with parameters in the super class Vehicle. So, we must define an explicit constructor in the subclass Car, and we must invoke the super class constructor from this constructor by using the super keyword. Now let's add it.
We can add the constructor by selecting Add Constructor in the Quick Fix Dialogue of Eclipse. Eclipse automatically added a constructor with a super keyword. Notice that after the keyword super, there is a parenthesis, and in the parenthesis, there are parameters in the Vehicle class. This is called a super call; let me add this as a comment. Calling the constructor of the super class. In other words, when we try to create an object from the Car class, it will first ask us for the parameters here, then when we enter the required parameter values, it will assign these parameter values to the relevant properties of the super class specified here. Thanks to the getter method in the Vehicle class, we will be able to take the assigned values and use them, okay? Let's continue now. Now I will declare a variable isAutomatic with boolean type in the class Car, and its initial value can be false.
Also, I will add the variable isAutomatic to the constructor of the Car class as the 4th parameter, and I will assign the parameter variable isAutomatic to the variable isAutomatic of this class. This .isAutomatic = isAutomatic. Also, I will need to generate the getter method of the variable isAutomatic to access it from the main method. We generate the getter method of this variable by using the same way. And lastly, I will save the code. Now, let's create a test class named SuperThisTest in the same package. And I will check the check box for the main method and click the 'Finish' button. In the main method, I will create a car object. Car mycar = new Car, and as you can see, it is waiting for us to enter parameters. So, I will enter them. The first parameter, "Car", represents the type of car. The second parameter, "Ferrari", represents the model of the car. The third parameter, 320, represents the max speed of the car, and last parameter, true represents that isAutomatic of the car.
We print the properties of Car class by using print methods. First, I will display the type of car S.out."Type" +, and after the plus sign, I will invoke the get type method from the super class vehicle. So, I write myCar.getType. Thus thanks to the getType() method, we can get the value of car, which is the value passed to the type variable in the Vehicle class. Now let's get the other values. In the second print method, I will display the model of the car, and I will invoke the getModel() method from super class Vehicle. In the third print method, I will display the maxSpeed of the car and I will invoke the getMaxSpeed() method from super class Vehicle. In the last print method, I will display is car automatic or not, but this time I will invoke the isAutomatic() method from subclass Car because we defined that isautomatic variable and its getter method in the Car class. Okay, let's run the code.
You see the results in the console. Type is car, Model is Ferrari, and Max Speed is 320. These properties come from super class Vehicle, but car isAutomatic true comes from subclass Car. The thing to keep in mind is that with the keyword super, you point to a super class. But with this keyword, you point to the class you are in. All right, so let's take a short break here, we will continue in the next video, and I'll see you there.
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.