Why a Constructor can not be final, static or abstract in Java?
Last Updated :
31 Mar, 2022
Prerequisite: Inheritance in Java
Constructor in java is a special type of method which is different from normal java methods/ordinary methods. Constructors are used to initialize an object. Automatically a constructor is called when an object of a class is created. It is syntactically similar to a method but it has the same name as its class and a constructor does not have a return type.
Java constructor can not be final
One of the important property of java constructor is that it can not be final. As we know, constructors are not inherited in java. Therefore, constructors are not subject to hiding or overriding. When there is no chance of constructor overriding, there is no chance of modification also. When there is no chance of modification, then no sense of restricting modification there. We know that the final keyword restricts further modification. So a java constructor can not be final because it inherently it cannot be modified. Also, a java constructor is internally final. So again there is no need for final declaration further.
Example: Suppose we are declaring a java constructor as final, now let's see what is happening.
Java
// Java Constructor as final
import java.io.*;
class GFG {
// GFG() constructor is declared final
final GFG()
{
// This line can not be executed as compile error
// will come
System.out.print(
"Hey you have declared constructor as final, it's error");
}
}
class Main {
public static void main(String[] args)
{
// Object of GFG class created
// Automatically GFG() constructor called
GFG obj = new GFG();
}
}
Output:
prog.java:4: error: modifier final not allowed here
final GFG( )
^
1 error
From the above example also it is clear that if we are defining constructor as final the compiler will give an error as modifier final not allowed.
Java constructor can not be static
One of the important property of java constructor is that it can not be static. We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor. Another thing is that if we will declare static constructor then we can not access/call the constructor from a subclass. Because we know static is allowed within a class but not by a subclass.
Example:
Java
// java class and a subclass
import java.io.*;
class GFG {
public GFG()
{
// Constructor of GFG class
System.out.println("GFG Constructor");
}
}
class SubClass extends GFG {
SubClass()
{
// Constructor of SubClass class
// By default super() is hidden here
// So Super class i.e GFG class constructor called
System.out.println("Subclass Constructor");
}
public static void main(String args[])
{
// SubClass class object created
// Automatically SubClass() constructor called
SubClass obj = new SubClass();
}
}
OutputGFG Constructor
Subclass Constructor
Above example expresses that, when an object of subclass is created then Superclass constructor is called by Subclass constructor through constructor chaining. But if we make superclass constructor static then it can't be called by Subclass as above said static it is accessible within the class but not by the subclass.
One more important reason for not declaring the constructor as static is that, we know a static member is executed first in a program just like the main method which is static and executed first. But constructor is called each and every time when an object is created. But if we will declare it static then the constructor will be called before object creation. So in general if we will see static and constructor are opposite to each other if we want to assign initial values for an instance variable we can use constructor and if we want to assign static variables we can use static blocks.
Example: Suppose we are declaring a java constructor as static, now let's see what is happening.
Java
// java constructor as static
import java.io.*;
class GFG {
// GFG() constructor is declared static
static GFG()
{
// This line can not be executed as it compile error
// will come
System.out.print(
"Hey you have declared constructor as static, it's error");
}
}
class Main {
public static void main(String[] args)
{
// Object of GFG class created
// Automatically GFG() constructor called
GFG obj = new GFG();
}
}
Output:
prog.java:5: error: modifier static not allowed here
static GFG( )
^
1 error
From the above example also it is clear that if we are defining constructor as static the compiler will give an error as modifier static not allowed.
Java constructor can not be abstract
One of the important property of java constructor is that it can not be abstract. If we are declaring a constructor as abstract as we have to implement it in a child class, but we know a constructor is called implicitly when the new keyword is used so it can't lack a body and also it can not be called as a normal method. Also, if we make a constructor abstract then we have to provide the body later. But we know constructor can not be overridden so providing body is impossible. Hence, what we will do with this abstract constructor when we can not provide implementation to it.
Example: Suppose we are declaring a java constructor as abstract, now let's see what is happening
Java
// java constructor as static
import java.io.*;
abstract class GFG {
// GFG() constructor is declared abstract
abstract GFG()
{
// This line can not be executed as compile error
// will come
System.out.print(
"Hey you have declared constructor as abstract, it's error");
}
}
class Main {
public static void main(String[] args)
{
// Object of GFG class created
// Automatically GFG() constructor should be called
// But object creation in abstract class is error
GFG obj = new GFG();
}
}
Output:
prog.java:5: error: modifier abstract not allowed here
abstract GFG( )
^
prog.java:17: error: GFG is abstract; cannot be instantiated
GFG obj = new GFG();
^
2 errors
From the above example also it is clear that if we are defining constructor as abstract ,the compiler will give an error as modifier abstract not allowed.
Note: Java Interface can not have constructor but Abstract classes can have a constructor
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read