Difference between revisions of "Java/C3/Using-final-keyword/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 44: | Line 44: | ||
|- | |- | ||
| 00:46 | | 00:46 | ||
− | | First of all, we will learn what '''final | + | | First of all, we will learn what '''final''' keyword is. |
|- | |- | ||
Line 68: | Line 68: | ||
|- | |- | ||
| 01:22 | | 01:22 | ||
− | | So we will directly go to the '''Employee class''' of the project. | + | | So, we will directly go to the '''Employee class''' of the project. |
|- | |- | ||
Line 80: | Line 80: | ||
|- | |- | ||
| 01:40 | | 01:40 | ||
− | | We will initialize the variable '''name '''with value | + | | We will initialize the variable '''name '''with value "sneha". |
|- | |- | ||
Line 88: | Line 88: | ||
|- | |- | ||
| 01:48 | | 01:48 | ||
− | | We get compilation error | + | | We get compilation error: '''The final field Employee.name cannot be assigned''' |
|- | |- | ||
Line 96: | Line 96: | ||
|- | |- | ||
| 02:05 | | 02:05 | ||
− | | We can initialize a final variable only once. | + | | We can initialize a '''final''' variable only once. |
|- | |- | ||
| 02:08 | | 02:08 | ||
− | | So we will comment the method '''setName''' which modifies the variable '''name'''. | + | | So, we will comment the method '''setName''' which modifies the variable '''name'''. |
|- | |- | ||
Line 112: | Line 112: | ||
|- | |- | ||
| 02:19 | | 02:19 | ||
− | | Come to the '''main | + | | Come to the '''main''' method and comment the line '''manager.setName("Nikkita Dinesh");''' |
|- | |- | ||
Line 150: | Line 150: | ||
|- | |- | ||
| 03:02 | | 03:02 | ||
− | | Remove the initialization of '''final variable name.''' That is remove “'''sneha”.''' | + | | Remove the initialization of '''final variable name.''' That is, remove “'''sneha”.''' |
|- | |- | ||
Line 162: | Line 162: | ||
|- | |- | ||
| 03:14 | | 03:14 | ||
− | | We get the error | + | | We get the error: '''The final field Employee.name cannot be assigned''' |
|- | |- | ||
| 03:20 | | 03:20 | ||
− | | This is because, if the '''final''' variable is not | + | | This is because, if the '''final''' variable is not initialized, then only '''constructor''' can initialize it. |
|- | |- | ||
Line 182: | Line 182: | ||
|- | |- | ||
| 03:47 | | 03:47 | ||
− | | So we will type '''Employee, parentheses, open and close curly brackets''' and inside the curly brackets, | + | | So, we will type: '''Employee, parentheses, open and close curly brackets''' and inside the curly brackets, let's initialize the variable '''name''' with value '''sneha''' semicolon. |
|- | |- | ||
Line 218: | Line 218: | ||
|- | |- | ||
| 04:40 | | 04:40 | ||
− | | We get error | + | | We get error: '''The final field Employee.name cannot be assigned''' |
|- | |- | ||
Line 242: | Line 242: | ||
|- | |- | ||
| 05:17 | | 05:17 | ||
− | | So now we will create a '''static block.''' | + | | So, now we will create a '''static block.''' |
|- | |- | ||
| 05:20 | | 05:20 | ||
− | | For that, in the '''Employee '''class, come to the '''constructor | + | | For that, in the '''Employee '''class, come to the '''constructor '''we created. |
|- | |- | ||
| 05:26 | | 05:26 | ||
− | | Here instead of '''Employee | + | | Here, instead of '''Employee''' parenthesis , we will type '''static'''. We have created a '''static block'''. |
|- | |- | ||
Line 266: | Line 266: | ||
|- | |- | ||
| 05:52 | | 05:52 | ||
− | | Come to the method '''setEmail | + | | Come to the method '''setEmail''' in '''Employee''' class. |
|- | |- | ||
Line 284: | Line 284: | ||
| Now, come to the method''' setEmail'''. | | Now, come to the method''' setEmail'''. | ||
− | Inside the '''method''' we will type | + | Inside the '''method''', we will type: '''newEmail is equal to abc@gmail.com''' semicolon |
|- | |- | ||
Line 296: | Line 296: | ||
|- | |- | ||
| 06:35 | | 06:35 | ||
− | | We get error | + | | We get error: '''The final local variable newEmail cannot be assigned.''' |
|- | |- | ||
Line 304: | Line 304: | ||
|- | |- | ||
| 06:50 | | 06:50 | ||
− | | So | + | | So, let's remove the variable modification. |
|- | |- | ||
Line 320: | Line 320: | ||
|- | |- | ||
| 07:10 | | 07:10 | ||
− | | We get error | + | | We get error: '''class Manager overrides final method getDetails()'''. |
|- | |- | ||
Line 340: | Line 340: | ||
|- | |- | ||
| 07:39 | | 07:39 | ||
− | | '''private | + | | '''private''' methods are not inherited by the '''child class.''' |
|- | |- | ||
| 07:43 | | 07:43 | ||
− | | So we can add a method '''getDetails() '''in the '''child class'''. You can try this as an assignment. | + | | So, we can add a method '''getDetails() '''in the '''child class'''. You can try this as an assignment. |
|- | |- | ||
Line 352: | Line 352: | ||
|- | |- | ||
| 07:54 | | 07:54 | ||
− | | In '''Employee''' class, remove '''final''' keyword before ''' | + | | In '''Employee''' class, remove '''final''' keyword before method '''getDetails'''. |
|- | |- | ||
Line 364: | Line 364: | ||
|- | |- | ||
| 08:15 | | 08:15 | ||
− | | For that we will again create a '''constructor'''. So instead of '''static''' we will type | + | | For that, we will again create a '''constructor'''. So, instead of '''static''' we will type: '''Employee''' parentheses. |
|- | |- | ||
Line 376: | Line 376: | ||
|- | |- | ||
| 08:36 | | 08:36 | ||
− | | We get error '''Illegal modifier for the constructor in type Employee''' | + | | We get error: '''Illegal modifier for the constructor in type Employee'''. |
|- | |- | ||
| 08:42 | | 08:42 | ||
− | | This is because, '''constructor''' cannot be '''final''' since '''constructors''' cannot | + | | This is because, '''constructor''' cannot be '''final''' since '''constructors''' cannot be inherited. |
|- | |- | ||
Line 392: | Line 392: | ||
|- | |- | ||
| 08:57 | | 08:57 | ||
− | | Add '''final | + | | Add '''final''' keyword before the '''class Employee''' to make it '''final'''. |
|- | |- | ||
Line 400: | Line 400: | ||
|- | |- | ||
| 09:06 | | 09:06 | ||
− | | We get error | + | | We get error: '''The method setEmail is undefined for the type Manager'''. |
|- | |- | ||
| 09:12 | | 09:12 | ||
− | | To know the actual error, | + | | To know the actual error, let's come to '''TestEmployee '''class and comment the lines |
|- | |- | ||
Line 412: | Line 412: | ||
|- | |- | ||
| 09:28 | | 09:28 | ||
− | | '''Save''' the class and '''run''' the program | + | | '''Save''' the class and '''run''' the program. |
|- | |- | ||
Line 420: | Line 420: | ||
|- | |- | ||
| 09:40 | | 09:40 | ||
− | | Here '''Manager class''' extends '''Employee class.''' | + | | Here, '''Manager class''' extends '''Employee class.''' |
|- | |- | ||
| 09:45 | | 09:45 | ||
− | | So let's come back to''' Employee''' class and remove the '''final '''keyword. Save the class. | + | | So, let's come back to''' Employee''' class and remove the '''final '''keyword. Save the class. |
|- | |- | ||
Line 440: | Line 440: | ||
|- | |- | ||
| 10:12 | | 10:12 | ||
− | | Now let's summarize. | + | | Now let's summarize. In this tutorial, we learnt: |
− | In this tutorial we learnt: | + | |
− | When to invoke final keyword | + | * When to invoke final keyword |
− | What are | + | |
− | '''final variables, | + | * What are '''final variables, final methods '''and '''final classes'''. |
− | + | ||
|- | |- | ||
Line 451: | Line 450: | ||
| As an assignment, | | As an assignment, | ||
− | Repeat the steps of '''Using final keyword '''tutorial for the '''Bike''' and '''Vehicle | + | Repeat the steps of '''Using final keyword '''tutorial for the '''Bike''' and '''Vehicle''' class which we used in the previous tutorial. |
|- | |- | ||
Line 459: | Line 458: | ||
|- | |- | ||
| 10:41 | | 10:41 | ||
− | | Watch the video available at the following link. It summarizes the Spoken Tutorial project | + | | Watch the video available at the following link. It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 10:47 | | 10:47 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: |
− | Conducts workshops | + | * Conducts workshops |
− | Gives certificates to those who pass an online test | + | * Gives certificates to those who pass an online test. |
− | For more details, please write to us | + | For more details, please write to us. |
|- | |- | ||
| 10:56 | | 10:56 | ||
|Spoken Tutorial Project is supported by the National Mission on Education through ICT, MHRD, Government of India. | |Spoken Tutorial Project is supported by the National Mission on Education through ICT, MHRD, Government of India. | ||
− | |||
That's it for this tutorial. This is Trupti Kini from '''IIT Bombay'''. Thank you very much for watching. | That's it for this tutorial. This is Trupti Kini from '''IIT Bombay'''. Thank you very much for watching. | ||
|} | |} |
Revision as of 16:50, 17 May 2017
Time | Narration |
00:01 | Welcome to the spoken-tutorial on Using final keyword. |
00:05 | In this tutorial, we will learn about the final keyword and when to invoke it. |
00:11 | We will also learn about -
|
00:18 | To record this tutorial, I am using:
|
00:30 | To follow this tutorial, you must have basic knowledge of Java and Eclipse IDE. |
00:36 | You must have knowledge of Subclassing and Method overriding. |
00:41 | If not, for relevant Java tutorials, please visit our website. |
00:46 | First of all, we will learn what final keyword is. |
00:50 | final is a keyword or reserved word in Java. |
00:55 | It can be applied to variables, methods or classes. |
01:01 | Now, let us learn what final variable is. |
01:05 | final variable is a variable whose value cannot be changed. That is, it will be a constant. |
01:13 | I am switching to Eclipse IDE now. I had already created a project named MyProject in the previous tutorial. |
01:22 | So, we will directly go to the Employee class of the project. |
01:26 | Come to the variable name. |
01:30 | Add final keyword before the variable name. We have made the variable name as final. |
01:40 | We will initialize the variable name with value "sneha". |
01:45 | We will Save and Run the program. |
01:48 | We get compilation error: The final field Employee.name cannot be assigned |
01:55 | This is because here the final variable name is already declared and initialised. |
02:05 | We can initialize a final variable only once. |
02:08 | So, we will comment the method setName which modifies the variable name. |
02:14 | Save the class. |
02:16 | Now, come to TestEmployee class. |
02:19 | Come to the main method and comment the line manager.setName("Nikkita Dinesh"); |
02:26 | We commented this line as it was instance of method setName. |
02:31 | We have already commented setName method in Employee class. |
02:35 | Now let us Save the class and Run the program. |
02:38 | Great!!!
We got the output, Name: Sneha Email: abc@gmail.com Manager of: Accounts |
02:47 | We got this output as we already initialized variables with these values in
TestEmployee class and Employee class. |
02:58 | Now come to the final variable name in Employee class. |
03:02 | Remove the initialization of final variable name. That is, remove “sneha”. |
03:08 | Uncomment the method setName. |
03:12 | Save and Run the program. |
03:14 | We get the error: The final field Employee.name cannot be assigned |
03:20 | This is because, if the final variable is not initialized, then only constructor can initialize it. |
03:28 | That is, it cannot be modified anywhere else in the program. |
03:33 | For that, let’s create a constructor in the Employee class. We have already learnt what a constructor is earlier. |
03:43 | We know that constructor has the same name as class name. |
03:47 | So, we will type: Employee, parentheses, open and close curly brackets and inside the curly brackets, let's initialize the variable name with value sneha semicolon. |
04:08 | Comment the method setName. |
04:12 | Save and Run the program. |
04:15 | We got the desired output. |
04:17 | final variable is successfully initialized in a constructor. |
04:22 | Now we will learn about final static variables. |
04:26 | Come to the final variable in Employee class. |
04:30 | Add static keyword before final keyword. We have made the final variable as static. |
04:38 | Save and Run the program. |
04:40 | We get error: The final field Employee.name cannot be assigned |
04:46 | This is because static final variables cannot be initialized in the constructor. |
04:53 | They must be assigned a value with their declaration. Or they must be declared in a static block. |
05:01 | static variables are shared among all the objects of a class. |
05:06 | Creating a new object would change the static variable. This is not allowed if the static variable is final. |
05:14 | Switch back to Eclipse IDE. |
05:17 | So, now we will create a static block. |
05:20 | For that, in the Employee class, come to the constructor we created. |
05:26 | Here, instead of Employee parenthesis , we will type static. We have created a static block. |
05:35 | Now we will Save and Run the program. |
05:38 | We got the desired output. We have successfully initialized static final variable. |
05:46 | Now let’s use final variable as a parameter to the method. |
05:52 | Come to the method setEmail in Employee class. |
05:55 | Add final keyword before String newEmail. We have made the parameter as final. |
06:03 | Save and Run the program. |
06:06 | We got the desired output. |
06:09 | Now, come to the method setEmail.
Inside the method, we will type: newEmail is equal to abc@gmail.com semicolon |
06:28 | We have modified the final variable newEmail. |
06:32 | Once again we will Save & Run the program. |
06:35 | We get error: The final local variable newEmail cannot be assigned. |
06:42 | This is because final variable as parameter to a method cannot be modified by that method. |
06:50 | So, let's remove the variable modification. |
06:54 | Now we will learn about final method. Come to the method getDetails in employee class. |
07:01 | Add final keyword before method getDetails. We have made the method as final. |
07:08 | Save and Run the program. |
07:10 | We get error: class Manager overrides final method getDetails(). |
07:16 | Come to the method getDetails() in Manager class. |
07:21 | This is because if you make any method as final, you cannot override it. |
07:29 | Manager class method getDetails overrides getDetails method in Employee class. |
07:36 | What if final method is private? |
07:39 | private methods are not inherited by the child class. |
07:43 | So, we can add a method getDetails() in the child class. You can try this as an assignment. |
07:51 | Switch back to Eclipse IDE. |
07:54 | In Employee class, remove final keyword before method getDetails. |
08:03 | Remove static keyword before final variable name. |
08:10 | Now, we will learn whether constructor can me declared as final or not. |
08:15 | For that, we will again create a constructor. So, instead of static we will type: Employee parentheses. |
08:26 | Add final keyword before the constructor. |
08:31 | Save and run the program. |
08:36 | We get error: Illegal modifier for the constructor in type Employee. |
08:42 | This is because, constructor cannot be final since constructors cannot be inherited. |
08:50 | We will remove the final keyword before the constructor. |
08:54 | Now, we will learn about final class. |
08:57 | Add final keyword before the class Employee to make it final. |
09:03 | Save and Run the program. |
09:06 | We get error: The method setEmail is undefined for the type Manager. |
09:12 | To know the actual error, let's come to TestEmployee class and comment the lines |
09:21 | manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts"); |
09:28 | Save the class and run the program. |
09:31 | The actual error is The type manager cannot subclass the final class Employee. |
09:40 | Here, Manager class extends Employee class. |
09:45 | So, let's come back to Employee class and remove the final keyword. Save the class. |
09:54 | Come to the TestEmployee class. Uncomment the lines manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts"); |
10:06 | Save the class and run the program. |
10:09 | We got the desired output. |
10:12 | Now let's summarize. In this tutorial, we learnt:
|
10:27 | As an assignment,
Repeat the steps of Using final keyword tutorial for the Bike and Vehicle class which we used in the previous tutorial. |
10:37 | Write down the classes in Java that are final classes. |
10:41 | Watch the video available at the following link. It summarizes the Spoken Tutorial project. |
10:47 | The Spoken Tutorial Project Team:
For more details, please write to us. |
10:56 | Spoken Tutorial Project is supported by the National Mission on Education through ICT, MHRD, Government of India.
That's it for this tutorial. This is Trupti Kini from IIT Bombay. Thank you very much for watching. |