Although you would have written many constructors before, I am sure you will find some suprises in following facts about constructors. To start with a constructor is nothing but a piece of code of class with same name as that of class which is called whenever an object is created of that class. Now understand following things one by one...
1.If you don't write a constructor for yourself the compiler will insert one - this is call a default constructor. Constructors can have arguments and there can be a no-argument constructor. Compiler inserted constructor will always be a no-arg constructor.
2. Constructors can take any number of arguments, even var-args i.e.: variable arguments. (More on this in a few days.)
3. Constructors cannot be marked final or static or abstract. Because final, static and abstract keywords are for methods, variables and classes. You can consider that constructors implicitly have final kind of behaviour because they are never inherited! They are not methods beware!
4. Constructors cannot have a return type! This is a very important and perhaps the only criteria to recognise quickly whether the given entity is a method or constructor. A method should have return type but a constructor should never have a return type. A method can have same name as that of the class - its perfectly legal! :)
5. Constructors are never inherited! Again...they aren't methods!! You can remember like this - "Constructors are not passed to the younger generation."
6. Whenever a object has to be created then the constructor of that class has to get called and also the constructors of all its super classes should get called. For example if A is parent of B and B is parent of C then to create an object of C we will call C's constructor which in turn will automatically call the constructors of class B and which inturn will call A's constructor and which inturn will call Object's constructor - as by default class A is child of class Object! Remember that the automatically inserted super class constructor call will be always to the no-arg constructor. If you find it a bit difficult then fire your questions in comments.
7. Constructors can use any access modifier. They can use private, protected and public access modifiers. And if nothing is written they have the default one- package level access. Start thinking why anyone would want to make a private constructor! :)
8. Constructors can be over loaded and hence there can be more than one constructors for a single class. A constructor can call other constructor by the keyword this. A call to another constructor with single argument looks like this(i); .A constructor can even explicitly call its super class constructor by keyword super. A call to parent's constructor with no argument looks like super(); . The first statement of every constructor should be a call to this or super.
9. And here's the most stuning one - Abstract classes have constructors! They are called whenever any object of its subclass is created. So the bottom line is every class has a constructor.
10. Interfaces do not have a constructor. An interface is just a contract, a contract to fulfill the implementation of some methods, remember that.
11. You can have call to both this( ) and super( ) in the same constructor. You can have them one inside other, but not both in any single constructor.
Hoossh.. that's all what I can remember right now.. curse my neurotic RAM! If I find anything else I will update the list. If you have any question, then do comment..as you have seen I reply to every question asked.
C ya,
5 comments:
Thanks! Can you provide an example for 8 and 11. This helps me in understanding more on constructors
--Kiran
Hi Kiran, welcome again! :)
Ok here are examples for point 8 and 11...
My 8th point was:
. Constructors can be over loaded and hence there can be more than one constructors for a single class. A constructor can call other constructor by the keyword this. A call to another constructor with single argument looks like this(i); .A constructor can even explicitly call its super class constructor by keyword super. A call to parent's constructor with no argument looks like super(); . The first statement of every constructor should be a call to this or super.
Here is an example..
class bike
{
bike(){
System.out.println("Its a bike!!");
}
}
class hayabusa extends bike
{
String name;
hayabusa()
{
super();
System.out.println("Yeh..its hayabusa!"):
}
hayabusa(String s)
{
this();
name= s;
}
}
I cant give indents kiran, so code isnt readable, i hope you get the things.. the super call is call for the no-arg constructor of super class. When i say this() i am calling co-arg constructor of same class.
Now example for 11:
My 11th point was:
You can have call to both this( ) and super( ) in the same constructor. You can have them one inside other, but not both in any single constructor.
Its pretty simple.. I have already called this() and super() in above example..now here you should remember that you can call both of them from a single constructor.. so following thing is illegal.
class bike
{
bike(){
System.out.println("Its a bike!!");
}
}
class hayabusa extends bike
{
String name;
hayabusa()
{
super();
System.out.println("Yeh..its hayabusa!"):
}
hayabusa(String s)
{
this();
super(); //Illegal!!
name= s;
}
}
Illegal because if you call super() and this() then they should be first statements in the constructor body and also because you cant call both of them from single consttructor.
I hope you get this kiran,if you have any doubts go ahead, i will try my best to explain them.
ANd I see that you are regular visitor and commentor on this blog, feels nice to get dedicated visitors like you.
C ya!
Bye
Thanks for the elucidation Saurabh! I should work with some examples of these types..
Hi! I'm a brazillian java developer, and I'm studying to SCJP version 6... I'm posting about the exame im my blog too, but in portuguese... :)
Talking with some friends, I decide put some code examples, because they can copy and paste in a notepade "of life" (brazillian expression), compile, and see theyselves the strange error message generated by java compiler :)
I arrived in your blog searching on google:
"Constructors are never inherited" java why.
Good Luck :D
Also one more important point is : Anonymous class can't have a Constructor.
Post a Comment