SlideShare a Scribd company logo
1 of 2
In java, the final keyword can be used with variable, method, or class. We have already
discussed the final variable and final method in java. In this post, we will discuss what is a
final class, and how to use it? What is the need for the final class in java?
Here is the table content of the article will we will cover this topic.
1. What is the final class in Java?
2. How to create a final class?
3. Advantages of the final class?
4. When to use a final class?
What is the final class in Java?
A class that is declared with the final keyword is known as the final class. A final class can’t be
inherited by subclasses. By use of the final class, we can restrict the inheritance of class. We can
create a class as a final class only if it is complete in nature it means it must not be an abstract
class. In java, all the wrapper classes are final class like String, Integer, etc.
If we try to inherit a final class, then the compiler throws an error at compilation time. We can’t
create a class as immutable without the final class.
How to create it?
We can create a final class by use of final keyword during the declaration of the class. We must
need to take the class should be complete. It should not an abstract class.
accessModifier final class className
{
// Body of class
}
Here accessModifierspecifies the scope of the method. You can read about access modifiers.
final is a keyword that is used to make a final method.
returnType is the type of data that you want to return from the method.
className is the name of class.
final class ParentClass
{
void showData()
{
System.out.println("This is a method of final Parent class");
}
}
//It will throw compilation error
class ChildClass extends ParentClass
{
void showData()
{
System.out.println("This is a method of Child class");
}
}
class MainClass
{
public static void main(String arg[])
{
ParentClass obj = new ChildClass();
obj.showData();
}
}
Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: Type
mismatch: cannot convert from ChildClass to ParentClass at
MainClass.main(MainClass.java:21)
Advantage of the final class
To provide Security: We can protect sensitive information from unauthorized access. Suppose
we have a non-final class that has some sensitive data that should be accessed by only authorized
persons. A hacker can inherit that class in the subclass and can make a substitute for it. Now
subclass has the methods our class and it looks and feels like the original class. It can damage
our sensitive data, so we can prevent this and provide security to our class.
To prevent this, we can declare the class with the final keyword so that no one could inherit it. If
you try to compile a subclass that inheriting a final class, then the compiler will show an error at
compile time.
To make Immutable class: To create an immutable class then the final keyword is mandatory.
It means an immutable class is always a final class.
When to use a final class?
1. A final class is introduced in JDK to prevent the inheritance of class. Suppose I have a class
that having some personal or secured information and you want that class should not be extended
by any class. Then you should use the final keyword with class.
2. To create an immutable class then the final keyword is mandatory. It means an immutable
class is always a final class.

More Related Content

What's hot (13)

Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Java basics
Java basicsJava basics
Java basics
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
 
Beauty of metaprogramming
Beauty of metaprogrammingBeauty of metaprogramming
Beauty of metaprogramming
 
Assigment 2
Assigment 2Assigment 2
Assigment 2
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
When to use abstract class and methods in java
When to use abstract class and methods in java When to use abstract class and methods in java
When to use abstract class and methods in java
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.
 
Common errors
Common errorsCommon errors
Common errors
 

Similar to Final Class in Java - By JavaGoal

this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdfParvizMirzayev2
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdfvenud11
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core ParcticalGaurav Mehta
 
PPT OF JAWA (1).pdf
PPT OF JAWA (1).pdfPPT OF JAWA (1).pdf
PPT OF JAWA (1).pdfNitish Banga
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questionsMehtaacademy
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfbca23189c
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulationİbrahim Kürce
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 

Similar to Final Class in Java - By JavaGoal (20)

this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
Java Core
Java CoreJava Core
Java Core
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
PPT OF JAWA (1).pdf
PPT OF JAWA (1).pdfPPT OF JAWA (1).pdf
PPT OF JAWA (1).pdf
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهMohamed Sweelam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 

Final Class in Java - By JavaGoal

  • 1. In java, the final keyword can be used with variable, method, or class. We have already discussed the final variable and final method in java. In this post, we will discuss what is a final class, and how to use it? What is the need for the final class in java? Here is the table content of the article will we will cover this topic. 1. What is the final class in Java? 2. How to create a final class? 3. Advantages of the final class? 4. When to use a final class? What is the final class in Java? A class that is declared with the final keyword is known as the final class. A final class can’t be inherited by subclasses. By use of the final class, we can restrict the inheritance of class. We can create a class as a final class only if it is complete in nature it means it must not be an abstract class. In java, all the wrapper classes are final class like String, Integer, etc. If we try to inherit a final class, then the compiler throws an error at compilation time. We can’t create a class as immutable without the final class. How to create it? We can create a final class by use of final keyword during the declaration of the class. We must need to take the class should be complete. It should not an abstract class. accessModifier final class className { // Body of class } Here accessModifierspecifies the scope of the method. You can read about access modifiers. final is a keyword that is used to make a final method. returnType is the type of data that you want to return from the method. className is the name of class. final class ParentClass { void showData() { System.out.println("This is a method of final Parent class"); } } //It will throw compilation error class ChildClass extends ParentClass { void showData() { System.out.println("This is a method of Child class"); } } class MainClass { public static void main(String arg[])
  • 2. { ParentClass obj = new ChildClass(); obj.showData(); } } Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from ChildClass to ParentClass at MainClass.main(MainClass.java:21) Advantage of the final class To provide Security: We can protect sensitive information from unauthorized access. Suppose we have a non-final class that has some sensitive data that should be accessed by only authorized persons. A hacker can inherit that class in the subclass and can make a substitute for it. Now subclass has the methods our class and it looks and feels like the original class. It can damage our sensitive data, so we can prevent this and provide security to our class. To prevent this, we can declare the class with the final keyword so that no one could inherit it. If you try to compile a subclass that inheriting a final class, then the compiler will show an error at compile time. To make Immutable class: To create an immutable class then the final keyword is mandatory. It means an immutable class is always a final class. When to use a final class? 1. A final class is introduced in JDK to prevent the inheritance of class. Suppose I have a class that having some personal or secured information and you want that class should not be extended by any class. Then you should use the final keyword with class. 2. To create an immutable class then the final keyword is mandatory. It means an immutable class is always a final class.