

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens when you declare a method/constructor final in Java?
Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass’s final method from the subclass.
i.e. The purpose of making a method final is to prevent modification of a method from outside (child class).
Still, if you try to override a final method a compile time error is generated.
Example
interface Person{ void dsplay(); } class Employee implements Person{ public final void dsplay() { System.out.println("This is display method of the Employee class"); } } class Lecturer extends Employee{ public void dsplay() { System.out.println("This is display method of the Lecturer class"); } } public class FinalExample { public static void main(String args[]) { Lecturer obj = new Lecturer(); obj.dsplay(); } }
Output
Employee.java:10: error: dsplay() in Lecturer cannot override dsplay() in Employee public void dsplay() { ^ overridden method is final 1 error
Declaring constructors final
In inheritance whenever you extend a class. The child class inherits all the members of the superclass except the constructors.
In other words, constructors cannot be inherited in Java therefore you cannot override constructors.
So, writing final before constructors makes no sense. Therefore, java does not allow final keyword before a constructor.
If you try, make a constructor final a compile time error will be generated saying “modifier final not allowed here”.
Example
In the following Java program, the Student class has a constructor which is final.
public class Student { public final String name; public final int age; public final Student(){ this.name = "Raju"; this.age = 20; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { new Student().display(); } }
Compile time error
On compiling, the above program generates the following error.
Student.java:6: error: modifier final not allowed here public final Student(){ ^ 1 error
- Related Questions & Answers
- What happens when you do not declare a variable in JavaScript?
- Can we declare constructor as final in java?
- What Happens When You Quit Smoking?
- What happens when you download a Fake App?
- What happens when you control your mind?
- What happens when you add a double value to a String in java?
- How to declare a constructor in Java?
- Can we declare the main () method as final in Java?
- Can a constructor be made final in Java?
- Why a constructor cannot be final in Java?
- What is a final method in Java?
- Can we declare an abstract method final or static in java?
- Can we declare the method of an Interface final in java?
- What happens if we try to extend a final class in java?
- Why constructor cannot be final in Java