
- 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
Can a constructor be made final in Java?
No, a constructor can’t be made final.
A final method cannot be overridden by any subclasses. As mentioned previously, the final modifier prevents a method from being modified in a subclass.
The main intention of making a method final would be that the content of the method should not be changed by any outsider.
But, in inheritance sub class inherits the members of a super class except constructors.
In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors. Therefore, java does not allow final keyword before a constructor. If you try a compile time error is generated as in the following example.
Example
public class Sample { public static void main(String args[]){ int num; final public Sample(){ num = 30; } } }
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error, insert "enum Identifier" to complete EnumHeaderName Syntax error, insert "EnumBody" to complete BlockStatement Syntax error, insert ";" to complete Statement at newJavaExamples.Sample.main(Sample.java:6)
- Related Questions & Answers
- Why a constructor cannot be final in Java?
- Why constructor cannot be final in Java
- Can a constructor be overridden in java?
- Can a constructor be synchronized in Java?
- Can we declare constructor as final in java?
- Can a final class be subclassed in Java?
- Can a class in Java be both final and abstract?
- Can constructors be marked final, abstract or static in Java?
- Can a final keyword alone be used to define a constant in Java?
- How can Indian prisons be made secure?
- Checking if a string can be made palindrome in JavaScript
- What happens when you declare a method/constructor final in Java?
- Why can't a Java class be both abstract and final?
- Can a final variable be initialized when an object is created in Java?
- Can we inherit a final method in Java?
Advertisements