Constructor in java can be final

Why a constructor cannot be 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).

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

Источник

Can we declare constructor as final in java?

A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have the same name as their class and, have no return type.

There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.

Example

public class Example < public Example()< System.out.println("This is the constructor of the class example"); >public static void main(String args[]) < Example obj = new Example(); >>

Output

This is the constructor of the class example

Final method

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).

Example

In the following Java program, we are trying to override a final method.

class SuperClass < final public void display() < System.out.println("This is a method of the superclass"); >> public class SubClass extends SuperClass < final public void display() < System.out.println("This is a method of the superclass"); >>

Compile time error

On compiling, the above program generates the following error.

SubClass.java:10: error: display() in SubClass cannot override display() in SuperClass final public void display() < ^ overridden method is final 1 error

declaring constructor as 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 make 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.

Output

Student.java:6: error: modifier final not allowed here public final Student()< ^ 1 error

Источник

can we make a constructor final in java?

For methods, final keyword is used to prevent them to be overridden by subclass. Constructors are also the special kind of methods but as we know that constructor can not be inherited in subclass, hence there is no use of final keyword with constructor.

If we declare constructor as final, compiler will give compile time error. Please see the below example:
Example

public class Main { final Main(){ System.out.println("Final Constructor"); } public static final void main(String[] args) { Main obj1 = new Main(); } }
Main.java:11: error: modifier final not allowed here final Main(){ ^ 1 error

Main.java:11: error: modifier final not allowed here final Main()< ^ 1 error

Java interview questions on constructor

  • Default constructor
  • Does constructor return any value in java?
  • Is constructor inherited in java?
  • Can you make a constructor final in java?
  • Difference between constructor and method in java?
  • How to copy values from one object to another java?
  • How to overload constructor in java?
  • can you create an object without using new operator in java?
  • Constructor chaining in java
  • Parameterized constructor in java
  • Can we call subclass constructor from superclass constructor?
  • What happens if you keep return type for a constructor?
  • What is the use of private constructor in java?
  • Can a constructor call another constructor java?

Источник

Properties of Constructors in Java

A constructors in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method that is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called. There are certain properties of constructors in java.

Properties of Constructors

1. Constructor Should not have any return type

If we declare a return type for a constructor, JVM considers it a normal method. The basic aim of the constructor is to assign a value to the object. It implicitly returns the address of the object.

2. The access specifier of the constructor can be private, default, protected, public

If the access specifier of the constructor is private, then an object of the corresponding class can be created in the context of the same class but not in the context of another class. If the access specifier of the constructor is the default, then an object of the corresponding class can be created in the context of classes that are in the same package. Similarly, if the constructor is declared as protected, the object of the corresponding class can be created in the context of classes of the same package as well as inherited classes of other packages. And, if the constructor is declared as public, the object of the corresponding class can be created in the context of any class.

3. A constructor cannot be declared as final and synchronized

The main purpose of using the final keyword is to restrict the overriding of a method (basically overriding is done in the context of inherited classes). As there is no concept of overriding constructors, declaring the final is useless. Using synchronized for a constructor raises a syntax error. Because only the thread that creates an object should have access to it while it is being constructed.

Источник

Читайте также:  Phpbb debug php warning in file root
Оцените статью