calling a constructor..!!

                              Calling constructor


First of all we all know that for what constructor is used for to initialize the members of class right..!!

Same as we initialize normally or by method calling and in it we initialize but when constructor is called one answer we know generally when we define object that is when we instantiate the object at that time the constructor is called  what is the other method by can we call the constructor its by inheriting that class with other class and calling that classes constructor automatically the base class constructor will be called when we call the derived one.

for example lets take a class Abc now we want to call the constructor of class Abc like

Abc o1= new Abc( )

So constructor of Abc will be called automatically . 

Now question arises that if we didnt define the constructor than will it call any one ,,?

yess its a convention type thing that if we didn't define any constructor in class compiler defines costructor at compile time but if we define a constructor than its necessary for us to define it that is the default one by initlializing some values generally poeple keep 0 giving the values to variables 

SO now moving on to the other method that is the inheritance inherit the class Abc with say xyz

So my latest command is in java so would like to give example as 

class xyz extends Abc 

 xyz( )// this is the default constructor we defined 

}

xyz( parameter type 1)//this is a constructor having a single parameter 
{

}

}

Like wise you can construct any constructor with multiple parameter
now create the object of class xyz as

xyz b1 = new xyz( )// this will call a default constructor of class xyz

now as we inherted class xyz with class Abc then first the constructor xyz  will be called and from that the constructor of class Abc will be called but how for that we have a super keyword for it as you provide a super( ) keyword as soon as the execution pointer comes to the super keyword it calls the base constructor the super keyword must be mentioned as the first line of the derived constructor otherwise it will give an error.

One thing to notice about its not necessary that we have to provide the super keyword its by default kept by compiler so no need to worry about but the question comes when to put super keyword .

super keyword is necessary when we want to initialize the value of different variables specifically or we can say that to initialize the value of our choice suppose for example we can keep super (parametr value 1 , parameter value 2,...) all this values will be forwarded to the constructor of the base class note this super statement must be kept as the first statement .

By super keyword we can also call the method of base class ..


So providing conclusion to my topic for today is that we have two methods to call a constructor

1) BY the object of its own class

2) By inheritance (that is inherit the class with the other class and call the object of the derived one automatically the object of base will be called and if you want to pass any parameter than you have super( ) keyword ..)

Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another