View Discussion Improve Article Save Article Like Article 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.
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:
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.
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
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 errorsFrom 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
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass: public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } } Here is a subclass, called Subclass, that overrides printMethod(): public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } } Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following: Printed in Superclass. Printed in Subclass Subclass ConstructorsThe following example illustrates how to use the super keyword to invoke a superclass's constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle. Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own: public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } Invocation of a superclass constructor must be the first line in the subclass constructor. The syntax for calling a superclass constructor is or:With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem. If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent. |