The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.
- Inheritance in Java
- 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 friends. In this video, we will talk about the "final" keyword. As you learned in the inheritance section, inheritance allows you to reuse existing code. However, there are times when we need to limit some properties for a variety of reasons. The final keyword allows us to do this limitation. In this video, we will look at what the final keyword means for classes, methods, and variables. The final keyword can be implemented in classes, methods, and variables. Final classes cannot be extended. If we try to inherit from a final class, we get a compile error. Java core libraries have many final classes. Final methods cannot be overridden. When we design a superclass and we want that a method in this class shouldn't be overridden, we can make this method final. Final variables cannot be altered. We can initialize the final variable, but we cannot reassign a new value. So, final variables are used to create constant variables.
Now, let's go through some examples to help us understand better. In the object-oriented programming project, right click on the source folder and select new class. Specify package name as finalkeyword, and class name as Vehicle. The Vehicle class will be superclass. Let's declare an instance variable named model with string type in the Vehicle class. Let's declare a public void method showMaxSpeed. We print 'The max speed of the Vehicle is 180' message by using the print method. Save the code. All right, let's create a new subclass and I will right click on the 'finalkeyword' package and select new class. Specify the class name as Car. Our subclass Car extends superclass Vehicle. This means that subclass Car inherits the showMaxSpeed method and the model variable from the super class Vehicle. In the Car class, we override the max speed method of superclass Vehicle. We print the "Max speed of Car is 320" message by using the print method, and save the code. All right, now we can deal with the final keyword. We want to limit some properties by using the final keyword. So, let's open the Vehicle class.
For example, we can add the final keyword to the class definition of superclass Vehicle. You can write it before or after the access modifier. Either way, it's not a problem. All right, now the Vehicle class is a final class. Let's save the code. And now let's open the Car class and observe the changes. As you can see, we get a compilation error. Let's look at it by placing the mouse pointer on Vehicle. The error message is, 'The type Car cannot subclass the final class Vehicle'. So, final classes cannot be extended. We select the remove final modifier of the Vehicle class. As you can see, Eclipse automatically remove the final keyword. Now, let's make this method final. I add the final keyword after the access modifier. Now this method is the final method. Let's save the code. Now let's open the Car class and observe the changes. As you can see, we get a compilation error. Let's look at placing the mouse pointer on the showMaxSpeed method. The error message says, you cannot override the final method from Vehicle.
So, final methods cannot be overridden. We select the remove final modifier of vehicle.showMaxSpeed method and save the code. Let's create the main test class named final test in the same package. In the main method, we create a car object. Car my Car = new Car(); Before accessing the instance variable of the Vehicle class, let's open the Vehicle class and add the final keyword to the variable model. Also, we have to initialize this variable. Because the final keyword makes the variables constant, so the variables must contain a value and this value should be constant for the variable with the final keyword. So, we can initialize the string with double quotes and save the code. Let's open the main class again. If we try to assign the Ferrari value to the variable model, we get a compilation error. Let's look at it by placing the mouse pointer on the model variable.
The error message says, "The final field Vehicle.model cannot be assigned." So, the final variables cannot be altered. We select remove final modifier of model and save the code. Now we can access and change the instance of the superclass. Okay secondly, let's call the showMaxSpeed method. All right, let's run the code. You see the results in the console. Overridden showMaxSpeed method is invoked and it displays 'the max speed of the car is 320'. Okay, I think you understand the purpose of the final keyword and how it's used. Now, let's focus on the final variables and take a closer look at how the final keyword affects the variables. First of all, I want to get the codes here in the comment line. All right, now let's define a final static variable in the test class. public static final int COUNTER =5; We usually write the final variable with capital letters, and if it consists of two or more words, we separate the words with an underscore.
This is not mandatory, but it is a convention. Also, you can change the order of the public static and final keywords. You won't get any errors. Now let's increase the value of the counter variable in the main method and transfer it to another variable, int newCounter = COUNTER++. But as you can see, we got a compilation error. Because the counter variable is final and cannot be changed, this usage is not valid, but it is valid if you write it like this, newCounter = COUNTER; and int anotherCounter = newCounter++. If you notice the final variable counter is still the same, but we can transfer the value of the counter to another variable and work on that variable. We can also directly use the counter in mathematical operations. For example, the following usage is valid, int sum = COUNTER + 5. Because the value of the counter has not changed, it is still the same. Also, you can use the final int variables in the loops.
For example, for(int i = 0, i < COUNTER, i++). And let's print the value of the i on the console. System.out.println(i); Let's run and see. As you can see, we can print the values of the i, so we can use the constant variable inside the loops. Also, you can use it with arrays like that. String car = new String[COUNTER]; So, this usage is also valid. The size of this array will be five. And I'll show you another thing with an array list. If you remember, we used to set the size of the arrays first and then we couldn't change the size of the array later. But working with lists is not like that. So, you can add elements to a list later. Now, taking advantage of this feature of lists, I will tell you about the use of the final keyword with lists. For example, let's create a new array list, public final static ArrayList. I'm selecting ArrayList from the java.util package. Let this be an array list of strings.
Let the name of this list be my CarList. Now, let's initialize this. I write new ArrayList after the equal sign. Now let's try to add a few elements to this list in the main method. myCarList.add("Mercedes"); myCarList.add("BMW"); myCarList.add("Ferrari");. As you can see, it's possible to add elements to a list created with a final keyword, but this is not possible. myCarList = new ArrayList<>(); This is invalid because here an attempt is made to assign a new object to the variable myCarList and the final variables cannot be changed. That's why we get a compilation error here. All right, friends. So, that is how the final keyword is used. I hope it was useful for you. Let's take a short break here, and I'll see you in the next lesson.
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.