Constructor calling using 'this' and 'super'
One of the main use of this and super keyword is calling the constructor of current class and super class. Lets start with this
Constructor calling using 'this'
Here follows a simple program which contains two constructors
public class ThisSample { public ThisSample() { System.out.println("This is a basic constructor!!------------------!"); } public ThisSample(String parameter) { System.out.println("This is a Constructor with a parameter "+ parameter); } }
Here you can create the instance of ThisSample class by calling ThisSample()
constructor or by calling ThisSample(String parameter)
. But in both cases it will excecute only one constructor content. If you instantiate the object using ThisSample()
constructor then the output become
This is a basic constructor!!!
If you instantiate the object using ThisSample("String")
constructor then the output become
This is a Constructor with a parameter String
But assume if you have a class which containing more than one constructor but the logic inside all the constructor are dependent (Like ThisSample(String parameter)
is the continuation of ThisSample()
constructor). In this situation we need to use this keyword. Lets go to change the above method with the logic "When i create an instance of ThisSample class using the constructor ThisSample(String parameter)
it must need to excccute the contents inside default constructor (ThisSample()
) before excecuting the contents inside ThisSample(String parameter)
". For the complete code click Here
public class ThisSample { public ThisSample() { System.out.println("This is a basic constructor!!!"); } public ThisSample(String parameter) { this();/**It will call the constructor ThisSample()**/ System.out.println("This is a Constructor with a parameter "+ parameter); } }
Here if you instantiate the instance of ThisSample class using parametarised constructor (ThisSample("String")
) the output looks like follows
This is a basic constructor!!! This is a Constructor with a parameter String
Explore 'this' Constructor calling
Lets take this keyword with an advanced view. Here follows some advanced points regards this keyword.
Point - 1 Must use this keyword as the first statement of constructor else you got compilation error. For example if you change the parametarised (ThisSample(String parameter)
) constructor of above program like follows you got compilation error
public ThisSample(String parameter) { System.out.println("This is a Constructor with a parameter "+ parameter); this();/**ERROR- Construtor call must be the first statement in a constructor**/ }
Point - 2 You can't call more than one constructor from a single constructor using this keyword. Lets add one more addtional constructor to the above sample program. Then if you try to call both of this new created constructor and the default constructor on the ThisSample(String parameter)
then you got compilation error. On the following example you can findout that from ThisSample(String parameter)
i tried to call two constructors but i got compilation error on second line of ThisSample(String parameter)
constructor and the error become also same as previous point "Constructor call must be the first statement in a constructor".
public class ThisSample { public ThisSample() { System.out.println("This is a basic constructor!!!"); } public ThisSample(String parameter) { this();/**It will call the constructor ThisSample()**/ this("My String",10);/**THIS LINE YOU GOT THE ERROR**/ System.out.println("This is a Constructor with a parameter "+ parameter); } public ThisSample(String strParam, Integer intParam){ System.out.println("This is a two parameter constructor. Integer Parameter is "+intParam+" and String Parameter is "+strParam); } public static void main(String[] args) { ThisSample thisSample = new ThisSample("String"); } }
But you can change this error like following way
public class ThisSample { public ThisSample() { this("My String",10);/**Change the line to here**/ System.out.println("This is a basic constructor!!!"); } public ThisSample(String parameter) { this();/**It will call the constructor ThisSample()**/ System.out.println("This is a Constructor with a parameter "+ parameter); } public ThisSample(String strParam, Integer intParam){ System.out.println("This is a Constructor with Integer and String parameter. Integer Parameter is "+intParam+" and String Parameter is "+strParam); } public static void main(String[] args) { ThisSample thisSample = new ThisSample("String"); } }
Also here you must note the output of above program for getting an understand about which constructor call first. The output looks like follows.
This is a Constructor with Integer and String parameter. Integer Parameter is 10 and String Parameter is My String This is a basic constructor!!! This is a Constructor with a parameter String
Point - 3 You can't call constructor using this from a method. It must be from a constructor.
Constructor calling using 'super'
Lets go for the constructor calling using super keyword. by using super key word you can call the parent class constructor from a child class. Lets create two classes named Fruit and Apple also here Apple is the child class and Fruit is the parent class. Here follows the java code for both classes
/**Fruit Class**/ class Fruit{ public Fruit(){ System.out.println("Calling Fruit Default Construtor"); } } /**AppleFruit Class**/ public class AppleFruit extends Fruit{ public AppleFruit(){ System.out.println("Calling AppleFruit Default Construtor"); } public static void main(String[] args) { AppleFruit appleFruit = new AppleFruit(); } }
If you analyse the output, then you can find out that from the by default the AppleFruit()
calls its parent class construtor. Here is the output
Calling Fruit Default Construtor Calling AppleFruit Default Construtor
This means by default the AppleFruit() constructor calls its super class constructor. So the AppleFruit()
constructor code become same as the following code
public AppleFruit(){ super(); System.out.println("Calling AppleFruit Default Construtor"); }
The following program shows one more constructor in parent class and the child class.
/**Fruit Class**/ class Fruit{ public Fruit(){ System.out.println("Calling Fruit Default Construtor"); } public Fruit(String category){ System.out.println("Calling Fruit class category constructor "+ category); } } /**AppleFruit Class**/ public class AppleFruit extends Fruit{ public AppleFruit(){ System.out.println("Calling AppleFruit Default Construtor"); } public AppleFruit(String category){ System.out.println("Calling category constructor of AppleFruit and category is "+ category); } public static void main(String[] args) { AppleFruit appleFruit = new AppleFruit("kashmeri"); } }
Here when you run the program main will call the AppleFruit(String category)
constructor with parameter as "kashmeri" from there it will call the default constructor of its parent class Fruit()
. So the output become
Calling Fruit Default Construtor Calling category constructor of AppleFruit and category is kashmeri
But in this situation i am not expecting this output , need the parametarized constructor of superclass when calling the parameterized constructor of child class. So here i need to override the default parameter calling of child class. So here i just rewrite the code of AppleFruit(String category)
- constructor into following way
public AppleFruit(String category){ super(category); System.out.println("Calling category constructor of AppleFruit and category is "+ category); }Its output become
Calling Fruit class category constructor kashmeri Calling category constructor of AppleFruit and category is kashmeri
Explore 'super' Constructor calling
Before i am going to exploring the super constructor. i just going to modify the above example, try to find its output
/**Fruit Class**/ class Fruit{ public Fruit(){ System.out.println("Calling Fruit Default Construtor"); } public Fruit(String category){ this(); System.out.println("Calling Fruit class category constructor "+ category); } } /**AppleFruit Class**/ public class AppleFruit extends Fruit{ public AppleFruit(){ this("kashmeri"); System.out.println("Calling AppleFruit Default Construtor"); } public AppleFruit(String category){ super(category); System.out.println("Calling category constructor of AppleFruit and category is "+ category); } public static void main(String[] args) { AppleFruit appleFruit = new AppleFruit(); } }So here when calling the
AppleFruit()
constructor it will call the parametarised construtor AppleFruit(String category)
. In AppleFruit(String category)
- there is a call goes to parametarised constructor of its super class (Fruit(String category)
). Also from Fruit(String category)
there a call to Fruit default constructor. So the output become follows (Here you must remember about the order of printing. It must be very helpful in compitative exams and interviews)
Calling Fruit Default Construtor Calling Fruit class category constructor kashmeri Calling category constructor of AppleFruit and category is kashmeri Calling AppleFruit Default Construtor
Lets come to the points to remember
Point -1 By default parent class default constructor (constructor with no parameters) will called by a child class construtor
Point - 2 If you want to override the parent default constructor then you manually need to write it into child class using super keyword
Point -3If you already called this constructor calling in a child class constructor and those constructor will not call super class default constructor.
Point -4super constructor calling must be the first line of a constructor
Point -5If the parent class not have the default constructor then you must need to set the super class constructor explicitly
Here i am again do some changes in the above example. What i do is i just remove the default constructor from the fruit class. So in AppleFruit default constructor there is compilation error happends due to the absence of default constructor in super class. Can you suggest what are the ways to resolve this issue
/**Fruit Class**/ class Fruit{ public Fruit(String category){ System.out.println("Calling Fruit class category constructor "+ category); } } /**AppleFruit Class**/ public class AppleFruit extends Fruit{ public AppleFruit(){ /**How to solve the compilation problem on here**/ System.out.println("Calling AppleFruit Default Construtor"); } public AppleFruit(String category){ super(category); System.out.println("Calling category constructor of AppleFruit and category is "+ category); } public static void main(String[] args) { AppleFruit appleFruit = new AppleFruit(); } }
One way is explicitly add the super class parametrized constructor constructor in AppleFruit()
constructor like follows
public AppleFruit(){ super("kashmiri"); System.out.println("Calling AppleFruit Default Construtor"); }
Other way is call the AppleFruit parametarised constructor in AppleFruit()
constructor using this like follows
public AppleFruit(){ this("kashmiri"); System.out.println("Calling AppleFruit Default Construtor"); }
Also please note that the recursive calling of constructor cause compilation error. Take a look at on following program which results a compilation error.
public class ChildClass{ public ChildClass(){this(1);}//Recursive invocation problem public ChildClass(int i){this();}//Recursive invocation problem }
No comments:
Post a Comment