- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Make a class final in Java
A class can be made final by using the final keyword. The final class cannot be inherited and so the final keyword is commonly used with a class to prevent inheritance.
A program that demonstrates a final class in Java is given as follows:
Example
final class A { private int a = 15; public void printA() { System.out.println("Value of a = " + a); } } public class Demo { public static void main(String args[]) { A obj = new A(); obj.printA(); } }
Output
Value of a = 15
Now let us understand the above program.
The class A is a final class. This means that it cannot be inherited. It has a private data member a and a method printA() that displays the value of a. A code snippet which demonstrates this is as follows:
final class A { private int a = 15; public void printA() { System.out.println("Value of a = " + a); } }
In the main() method in class Demo, an object obj of final class A is created. Then printA() method is called. A code snippet which demonstrates this is as follows:
public class Demo { public static void main(String args[]) { A obj = new A(); obj.printA(); } }
- Related Articles
- Final class in Java
- Can a final class be subclassed in Java?
- Why Final Class used in Java?
- Can a class in Java be both final and abstract?
- Why String class is immutable or final in Java?
- Explain final class and final method in PHP.
- What happens if we try to extend a final class in java?
- Simulating final class in C++
- How to make a class singleton in Java?\n
- How to make a class thread-safe in Java?
- Can a method local inner class access the local final variables in Java?
- Why can't a Java class be both abstract and final?
- final keyword in Java
- Final variable in Java
- Final Arrays in Java

Advertisements