SlideShare a Scribd company logo
Final Keyword In Java
Mrs.S.KAVITHA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE
SRI SARADA NIKETAN COLLEGE OF SCIENCE FOR
WOMEN,KARUR
• The final keyword in java is used to restrict the
user.
java final keyword can be used in many context.
Final can be:
• variable
• method
• class
Java final variable
• If you make any variable as final, you cannot
change the value of final variable(It will be
constant).
Example of final variable
• There is a final variable speedlimit, we are going
to change the value of this variable, but It can't
be changed because final variable once assigned
a value can never be changed.
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
Output:Compile Time Error
Java final method
If you make any method as final, you cannot override it.
Java final method
If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error
Java final class
If you make any class as final, you cannot extend it.
Example of final class
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
Output:Compile Time Error

More Related Content

PPTX
BCA Final Keyword.pptx
PPT
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
PPTX
Java Final Keyword
PPT
Final Class Final exam in the evening Methods.ppt
PPT
Final keyword
PPT
Final keyword in java
PPT
Super and final in java
PPTX
Java final keyword
BCA Final Keyword.pptx
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
Java Final Keyword
Final Class Final exam in the evening Methods.ppt
Final keyword
Final keyword in java
Super and final in java
Java final keyword

Similar to The final keyword in java is used to restrict the user. (8)

PPT
‘ Final ‘ to avoid overriding
PPTX
Lecture 8.2 - Final Attribute, Final Method and Final Class.pptx
PPTX
Lecture_5_Method_overloading_Final.pptx in Java
PPTX
Lecture4_Method_overloading power point presentaion
PPTX
Lecture5_Method_overloading_Final power point presentation
PPTX
This keyword and final keyword
DOCX
Final Class in Java - By JavaGoal
PPTX
Top 10 java interview questions.
‘ Final ‘ to avoid overriding
Lecture 8.2 - Final Attribute, Final Method and Final Class.pptx
Lecture_5_Method_overloading_Final.pptx in Java
Lecture4_Method_overloading power point presentaion
Lecture5_Method_overloading_Final power point presentation
This keyword and final keyword
Final Class in Java - By JavaGoal
Top 10 java interview questions.
Ad

More from Kavitha S (9)

PPTX
data link layer is the protocol layer in a program that handles the moving of...
PPTX
Java provides statements that can be used to control the flow of Java code
PPTX
When a break statement is encountered inside a loop, the loop is immediately ...
PPTX
a variable in Java must be a specified data type
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
A constructor in Java is a special method that is used to initialize objects
PPTX
the array, which stores a fixed-size sequential collection of elements of the...
PPTX
A class which is declared with the abstract keyword is known as an abstract c...
PPTX
How to create a two-dimensional array in java
data link layer is the protocol layer in a program that handles the moving of...
Java provides statements that can be used to control the flow of Java code
When a break statement is encountered inside a loop, the loop is immediately ...
a variable in Java must be a specified data type
Inheritance in Java is a mechanism in which one object acquires all the prope...
A constructor in Java is a special method that is used to initialize objects
the array, which stores a fixed-size sequential collection of elements of the...
A class which is declared with the abstract keyword is known as an abstract c...
How to create a two-dimensional array in java
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
Lesson notes of climatology university.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Institutional Correction lecture only . . .
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
Lesson notes of climatology university.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharma ospi slides which help in ospi learning
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
human mycosis Human fungal infections are called human mycosis..pptx
Cell Types and Its function , kingdom of life
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
Sports Quiz easy sports quiz sports quiz
Institutional Correction lecture only . . .
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx

The final keyword in java is used to restrict the user.

  • 1. Final Keyword In Java Mrs.S.KAVITHA ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER SCIENCE SRI SARADA NIKETAN COLLEGE OF SCIENCE FOR WOMEN,KARUR
  • 2. • The final keyword in java is used to restrict the user. java final keyword can be used in many context. Final can be: • variable • method • class
  • 3. Java final variable • If you make any variable as final, you cannot change the value of final variable(It will be constant). Example of final variable • There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.
  • 4. class Bike9{ final int speedlimit=90;//final variable void run(){ speedlimit=400; } public static void main(String args[]){ Bike9 obj=new Bike9(); obj.run(); } }//end of class Output:Compile Time Error
  • 5. Java final method If you make any method as final, you cannot override it. Java final method If you make any method as final, you cannot override it. Example of final method class Bike{ final void run(){System.out.println("running");} } class Honda extends Bike{ void run(){System.out.println("running safely with 100kmph");} public static void main(String args[]){ Honda honda= new Honda(); honda.run(); } } Output:Compile Time Error
  • 6. Java final class If you make any class as final, you cannot extend it. Example of final class final class Bike{} class Honda1 extends Bike{ void run(){System.out.println("running safely with 100kmph");} public static void main(String args[]){ Honda1 honda= new Honda1(); honda.run(); } } Output:Compile Time Error