Java/C3/Using-final-keyword/English

From Script | Spoken-Tutorial
Revision as of 15:44, 5 August 2014 by Trupti (Talk | contribs)

Jump to: navigation, search

Title of script: USING FINAL KEYWORD

Author: Trupti Rajesh Kini

Keywords: video tutorial, final keyword, final variable, final method, final class


Visual Cue Narration
Slide 1 Welcome to the spoken-tutorial on Using final keyword.
Slide 2 In this tutorial we will learn about the final keyword and when to invoke it.


We will also learn about -

  • final variables,
  • final methods,
  • final classes


Slide 3

Software Requirements


To record this tutorial, I am using
  • Ubuntu Linux version 12.04
  • JDK 1.7
  • Eclipse 4.3.1


Slide 4

Prerequisites


To follow this tutorial, you must have,
  • Basic knowledge of Java and Eclipse IDE.
  • and knowledge of Subclassing and Method overriding.

If not, for relevant Java tutorials, please visit our website.

Slide 5

final keyword

First of all, we will learn what final keyword is.

final is a keyword or reserved word in Java.

It can be applied to variables, methods or classes.

What is final variable?

Slide 6

Now, let us learn what final variable is.


final variable is a variable whose value cannot be changed.

That is, it will be a constant.

Go to the Eclipse IDE >> Click on MyProject I am switching to Eclipse IDE now.


I had already created a project named MyProject in the previous tutorial.

Go to the Employee class So we will directly go to the Employee class of the project.
Point to the variable name Come to the variable name.
Type final keyword Add final keyword before the variable name.

We have made the variable name as final.

Type sneha We will initialize the variable name with value sneha.
Save and Run the program. We will Save and Run the program.
Highlight the error

cannot assign a value to final variable name

name=newName;

We get compilation error.


The final field Employee.name cannot be assigned

This is because here the final variable name is already declared and initialised.

We can initialize a final variable only once.

So we will comment the method setName which modifies the variable name.

Save the class

Go to TestEmployee class->main method

Type // before manager.setName("Nikkita Dinesh");

Now, come to TestEmployee class.


Come to the main method and comment the line manager.setName("Nikkita Dinesh");


We commented this line as it was instance of method setName.


We have already commented setName method in Employee class.



Save and Run the program. Now lets Save the class and Run the program.
Highlight the output

Name: Sneha

Email: abc@gmail.com

Manager of: Accounts

Great!!!

We got the output,

Name: Sneha

Email: abc@gmail.com

Manager of: Accounts

We got this output as we already initialized variables with these values in TestEmployee class and Employee class.
Go to Employee class->variable name Go to the Employee class.

Now come to the final variable name. in Employee class.

Remove the initialization “sneha” Remove the initialization of final variable name.

That is remove “sneha”.

Go to the method setName

Uncomment the method.

Uncomment the method setName.



Save and run the program. Save and Run the program.
Highlight the error

cannot assign a value to final variable name

name=newName;

We get the error,

The final field Employee.name cannot be assigned

This is because if the final variable is not initialised, then only constructor can initialize it.

That is it cannot be modified anywhere else in the program.

Go to Employee class For that, let’s create a constructor in the Employee class.


We have already learnt what a constructor is earlier.

We know that constructor has the same name as class name.
Type,

Employee() { name="Sneha";

}

SO we will type

Employee, parentheses, open and close curly brackets


and inside the curly brackets, lets initialize the variable name with value Sneha.

Point to the method setName

Enclose the method inside /* */

Comment the method setName.
Save and Run the program. Save and Run the program.
Highlight the output

Name: Sneha

Email: abc@gmail.com

Manager of: Accounts

We got the desired output.


final variable is successfully initialized in a constructor.
Now we will learn about final static variables.
Go to the Employee class Come to the final variable in Employee class.


Type static before final keyword Add static keyword before final keyword

We have made the final variable as static.

Save & Run the program. Save and Run the program.
Highlight the error

cannot assign a value to final variable name

name="Sneha";

We get error.

The final field Employee.name cannot be assigned

slides and document is not synchronized from this point and onward.

Further slide 9.1 and 9.2 [i.e. 25,26] contains one extra word block in second bullet,doneSlide 7

This is because static final variables cannot be initialized in the constructor.


They must be assigned a value with their declaration.


Or they must be declared in a static block.

Slide 8 static variables are shared among all the objects of a class.


Creating a new object would change the static variable.


This is not allowed if the static variable is final

Go to Eclipse IDE Switch back to Eclipse IDE.
Go to the Employee class ->constructor


Delete Employee and type static.

So now we will create a static block.


For that, in the Employee class, go to the constructor. we created


Here instead of Employee(), we will type static.

We have created a static block.

Now we will Save and Run the program. Now we will Save and Run the program.
Highlight the output,

Name: Sneha

Email: abc@gmail.com

Manager of: Accounts

We got the desired output,
We have successfully initialized static final variable.


Now let’s use final variable as a parameter to the method.
Go to the method setEmail Come to the method setEmail. in Employee class.
Type final before String newEmail Add final keyword before String newEmail

We have made the parameter as final.

Save and Run the program. Save and Run the program.
Highlight the output

Name: Sneha

Email: abc@gmail.com

Manager of: Accounts

We got the desired output,


Type newEmail="kinitrupti@gmail.com"; Now, come to the method setEmail.

Inside the method we will type,

newEmail is equal to inside double quotes abc@gmail.com semicolon.

We have modified the final variable newEmail.

Save & Run the program. Once again we will Save & Run the program.
Highlight the error,

final parameter newEmail may not be assigned

We get error.

The final local variable newEmail cannot be assigned.

This is because final variable as parameter to a method cannot be modified by that method.

So lets remove the variable modification. <<PAUSE>>

Now we will learn about final method.
Go to Employee class

Come to the method getDetails in employee class.

Come to the method getDetails in employee class.


Type final keyword before getDetails.

Add final keyword before method getDetails.


Save and Run the program. Save and Run the program.
Point to the compilation error,

Cannot override the final method from Employee

We get error,

class Manager overrides final method getDetails.()

Come to the method getDetails() in Manager class. This is because if you make any method as final, you cannot override it. Manager class method getDetails overrides getDetails method in Employee class.

Slide 9 What if final method is private?


private methods are not inherited by the child class.


So we can add a method getDetails() in the child class.


You can try this as an assignment.

Go to Eclipse IDE Switch back to Eclipse IDE.
Go to Employee class

In Employee class remove final keyword before method getDetails. Remove static keyword before final variable name.

Type,

Employee() { name="Sneha";

}


Now we will learn whether constructor can me declared as final or not

For that we will again create a constructor.

So instead of static we will type, Employee parentheses.


Type final before the constructor Add final keyword before the constructor.
Save and run the program. Save and run the program.
Highlight compilation error,

modifier final not allowed here

We get error

Illegal modifier for the constructor in type Employee

This is because constructor cannot be final since constructors are not inherited.

we will remove the final keyword before the constructor.

Now, we will learn about final class.


Type final keyword before the class Employee. Add final keyword before the class Employee. to make it final.
Save and Run the program. Save and Run the program.
Highlight the compilation error,

The type Manager cannot subclass the final class Employee

We get an error,

The method setEmail is undefined for the type Manager


Highlight the compilation error,

The type Manager cannot subclass the final class Employee

To know the actual error, lets come to TestEmployee class and comment the lines manager.setName("Nikita Dinesh"); manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts");

Save the class and run the program The actual error is the type manager cannot subclass the final class Employee. Here Manager class extends Employee class. So lets come back to Employee class and remove the final keyword. Come to the TestEmployee class Uncomment the lines manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts");

Save the class and run the program. We got the desired output.


Now lets summarize
Slide 11

Summary


In this tutorial we learnt:
  • When to invoke final keyword.
  • What are:
    • final variables,
    • final methods and
    • final classes


Slide 12

Assignment


As an assignment
    • Are you sure about the name of the tutorial? Pls chkThe bike vehicle was given as an assignment in the previous tutorial.
      We need to frame it properly.Pls frame it properly.doneRepeat the steps of Using final keyword tutorial for the Bike and Vehicle class.
    • Write down the classes in java that are final classes.


Slide 13

About Spoken Tutorial Project


Watch the video available at the following link http://spoken-tutorial.org/What is a Spoken Tutorial
  • It summarizes the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Slide 14

About Spoken Tutorial workshops


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact at spoken hyphen tutorial dot org


Slide 15

Acknowledgement


Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at

This is Trupti Kini from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Trupti