Here I explain the very basic details about java constructors. I explain those things with a question answer like pattern. So it will helps you on some interviews
What is the use of a Constructor in java?
Constructors are used to creating an instance (creating an object) of a class. The instances are created by using the 'new' keyword.
The following line shows the code for creating a new instance of MyClass Object by calling the MyClass() constructor.
MyClass myClass = new MyClass();
How a constructor looks like?
Constructors name must have same as the name of corresponding class. Also it doesn't have any return type. The constructor become execute when you create the new instance of a class using that corresponding constructor. Also in java there is two types of constructors Parametrized and default (non parametrized) constructors.
Parametrized Constructors - Those are the constructors which consist of parameters. Instance of the class can be created by calling the constructor with corresponding parameters. The code below shows the parametarized constructor of MyClass also it shows how to call this parametarized constructor in main method.
public class MyClass { public MyClass(int i){ } public static void main(String[] args) { MyClass myClass = new MyClass(1); } }
Non Parametrized or default Constructors - Those are the constructors which did't consist any parameters. If you are not expicitly initialized any constructor in your java class then the java become create a default constructor on the compile time. The following code snipplet shows a default constructor initialization and its calling.
public class MyClass { public MyClass(){ } public static void main(String[] args) { MyClass myClass = new MyClass(); } }
What is the difference between constructor and method?
The main difference difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.
The constructor can called with the help of new keyword which create an instance of corresponding class. But in the case of methods that can called directly on an object that already exist.
Also in the case of coding level constructors didn't have any return type but in methods it must have a return type. Constructors name must be same as the name corresponding class.
What become the output of code snippet?
class SuperClass{ public SuperClass(int i){} } class MyClass extends SuperClass{ public MyClass(){}//line number 5 }
The output become a Compailation error. Because in the above code the super class consist of an overrided parametarized constructor. By default in child class constructor it will call super class default constructor. So here in line number 5 MyClass Constructor tries to load the default constructor of SuperClass but it cant find out the default constructor. So here it cause a compilation error in line number 5 which is "Implicit super constructor SuperClass() is undefined. Must explicitly invoke another constructor". You can solve this issue by using anyone of following machanisms.
1. Add an explicit default constructor in SuperClass
class SuperClass{ public SuperClass(){} public SuperClass(int i){} } class ChildClass extends SuperClass{ public ChildClass(){} }
2. Explicitly call the parametrized constructor of SuperClass from ChildClass
class SuperClass{ public SuperClass(int i){} } class MyClass extends SuperClass{ public MyClass(){super(1);} }
How to call one constructor from another constructor?
It is possible to call one constructor from another constructor by using the keywords like this() and super(). this keyword calls another constructor in the current class and super keyword calls another constructor in the super class. Calls to other constructors in this way may only be done as the first line in a constructor. If you call another constructor of child class using this() keyword then those constructor will not calls the super class default constructor
No comments:
Post a Comment