Final Keyword

Start course
Overview
Difficulty
Beginner
Duration
2h 28m
Students
16
Description

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there, in this video we'll talk about the final keyword. As you understand in the inheritance subject, inheritance provides reusing existing code, but sometimes we need to limit some properties for several 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 class 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 super class and we want to make sure 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 re assign a new value. 

So, final variables are used to create constant variables. Let's make some examples to understand better. In the object-oriented programming project, right click on the source folder and select new class, specify the package name as finalkeyword and class name as vehicle. The vehicle class will be super class. 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. We saved the code. Let's create a new subclass. I will right click on the final keyword package and select new class, specify the class name as car. Our subclass car extends Super class vehicle. This means that subclass car inherits the showMaxSpeed method and the model variable from the super class vehicle. In the cars class we override the max speed method of super class vehicle. We print the max speed of car is 320 message by using the print method. We save the code. Now we can deal with the final keyword. We want to limit some properties by using the final keyword. 

Let's open the vehicle class. For example, we can add the final keyword to the class definition of super class vehicle. You can write it before or after the access modifier there is no problem. 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 removed final modifier of the vehicle class. As you can see Eclipse automatically removed 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 removed final modifier of vehicle.showMaxSpeed method and save the code. Let's create the main test class named finalTest in the same package. In the main method we create a car object. Car, myCar = 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 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 this 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 super class. Second let's call the showMaxSpeed method. 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. I think you understand the purpose of the final keyword and how it is 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. Yes, now let's define a final static variable in the test class. Public static final int COUNTER = 5. 

We usually write the final variables 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 will not 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 counter is not changed it's still the same. Also you can use the final int variables in the loops. For example, 4 int i = 0 i< the COUNTER i ++. And let's print the value of the eye on the console. S out printIn(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 [] cars = new String [COUNTER] this usage is also valid. The size of this array will be five and I'll show you another thing with an ArrayList. We haven't learned ArrayList yet. But by the way, I want to show you something small. 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'll tell you about the use of the final keyword with lists. For example, let's create a new ArrayList. 

Public final static ArrayList. I'm selecting ArrayList from the java.util package. Let this be an ArrayList of strings. Let the name of this list be myCarList. 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") myCarListdot.add ("Ferrari"). As you can see it's possible to add elements to a list created with the 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. Yes, friends this is how the final keyword is used. I hope it was useful for you. Let's take a short break, see you in the next lesson.

 

About the Author
Students
81
Courses
18
Learning Paths
1

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.

Covered Topics