Constructors in JAVA (TIPS)

JAVA Programming - Programming in JAVA language,Tips of JAVA Programming,JAVA Programming from Beginning
@suryan (45)
India
December 23, 2006 11:41am CST
Hi friends, This post is for java programmers who has done core JAVA.You must have learn objects.Students learning java only know when a object is created constructor is automatically called and the variables are intialised which are inside the constructor.Constructor can be overloaded also but not has return type but if we specify a return type it acts as a normal function.And this function can be called by object.If we are overloading constructor than then depending upon the argument passed through object particular constructor is overloaded. This was just a revesion.Now we will learn in deep.If there are two classes.suppose A and B .And if B is derived class and A is base class then the constructor of A will be virtually present in B class.As it is inherting.And when we try to create a object of derived class that is B.Do you know which constructor will invoke first.Constructor A or Constructor B.Don't get confuse.Most of them will tell constructor B will be invoked first.This is right but do you know the reason behind this.When constructor B is invoked there is a super keyword just below constructor B braces and now super keyword calls constructor A.After executing constructor A it then executes constructor B.So if you write a printf statment in Constructor A and Constructor B.Then printf statement in constructor A will get printed and then constructor B will get printed. And when we donot specify any constructor then default constructor are called and get intialised.But if you specify a construcor then default constructor will not invoke.And the constructor you specified will be invoked.So now I think you have got the whole idea about constructors.
No responses