Saturday, 31 October 2015

WHAT IS A CONSTRUCTOR AND WHAT ARE THE TYPES IN JAVA..??



INTRODUCTION:

      Heiii friends..In this post I am going to explain about constructors and its types in java..!! I will make sure that you will never ever forget of what is meant by constructors after reading this post..Continue reading to experience it..!!

    WHAT IS A CONSTRUCTOR..?

         Constructor is something that comes to life when  an object creation is done.

        Consider the example,

           Class Dad ( )
                {
                  public Dad( )
                      {
                         //this is the constructor
                      }
                 public static void main(String args[] )
                    {
                         Dad dadobj= new Dad( );
                    }

    As we all know.. dadobj is the object here but what the new code 

   public Dad( )
   {

    } 

      is doing..?.. Yes.. when the object creation is done ..ie as soon the reference variable ‘dadobj’ has stored the address of the class successfully .. and as soon the compiler sees "new " keyword in the right side.. the compiler assumes the object creation is done successfully and calls the “SON OF THE CLASS DAD”

         Wow.. Son is born for the class DAD and it will be automatically be born as soon as the compiler sees successful object creation..

         So.. SON of the class DAD is born but why the name is same for the new born constructor still??

        Hmm… same question is going on my mind too.. !! :D 

        All the class is soo  affectionate with their in born so they want the same name to be kept for their in born too..

       They initially had an argument with the compiler .. initially compiler refused but later it accepted..just to make all the classes happy :D

        So from then on..

The CONSTRUCTOR NAME MUST BE THE SAME AS THE CLASS NAME rule came into being..

    DEFAULT CONSTRUCTOR:

             I can give you the same above example for the default constructor.. because default constructor is the one that will be invoked when an object creation of the class is done..ie.. when the compiler sees “new” keyword 

          If you don’t know what an object creation is.. then please refer the post objects in java

      EXAMPLE FOR DEFAULT CONSTRUCTOR:
 
        Class Dad ( )
           {
              Public Dad( )
                 {
                 System.out.println(“Default constructor is called”);
                 }
              Public static void main(String args[] )
                {
                 Dad dadobj= new Dad( );
                }
          }
 
       OUTPUT:

       Default constructor is called

      PARAMETERISED CONSTRUCTOR:

           The Constructor with parameters are called “PARAMETERISED CONSTRUCTOR”

           Class Dad ( )
              {
                 Public Dad( )
                     {
                       System.out.println(“Default constructor is called”);
                      }
                   Public Dad(int x, int y )
                      {
                        Int sum = x+ y;
                        System.out.println(“parameterised constructor is called and sum is” +sum);
                       }

                   Public static void main(String args[] )
                    {
                        Dad dadobj= new Dad( );
                        Dad dadobj2 = new Dad(5,5 );
                    }

     OUTPUT:

         Default constructor is called
         parameterized constructor is called and sum is 10

     EXPLANATION:

         Initially object creation is done for the class DAD and then comes a point to call the constructor..

         Now .. since there are many constructors.. compiler will first check..like.. “ok I got to call an empty constructor..let me go and check..if there is any empty constructor inside the class” Is what the compiler plans..

          So..it goes inside the class and wow… "I got empty constructor..it doesn’t have any parameters inside.. so it is the default constructor and I will print whats got inside it.." is what the compiler doing

          Next another object is created for the same class and now parameters are passed inside the constructor:

         COMPILER: Oh.. man I got to search for the constructors which has got 2 parameters..
Let me search one by one..

          Oh.. Its an empty constructor.. I cant execute it..

         Let me check with the second constructor..

          Arey..wow.. I got a parameterized constructor but I must see whether the datatype Is correct and number of parameters are correct..

          I need 2 parameters and datatype of both is int,int

          Wow.. its really a good thing..I just got the same constructor.. I need not go down and search futher.. :D

          Im ssoooo happy !!

          So now the compiler is happy and so the clean output

           Parameterized constructor is not only with 2 parameter..it can have any number of parameter .. so every time the compiler will go..grab the right constructor and will compile..

       RULES FOR CONSTRUCTOR:


  •         Constructor name must be the same as the class name



  •        Constructor can have any access specifier(PUBLIC,PRIVATE etc) ie. it can have PUBLIC , PRIVATE or PROTECTED keyword in the front..



  •         It must not have a return type .. ie. return keyword should not be used inside the constructor



  •         A class can have more than one constructor.. We have seen above.. a class which is having 2 constructor.. one is default and other is parameterized..



          CONCLUSION:

                Soo… folks.. hope you are aware of what  a constructor actually mean.. !! Please hit like button..drop your valuable feedbacks below ..and help me post more.. !!! Stay tuned and support for more posts..!!
Suggestions and request the post you wish is also welcomed. !! Hope you understood the concepts better.. Have a blissful day :)

WHAT IS A METHOD AND WHAT ARE THE TYPES OF METHOD IN JAVA..??



INTRODUCTION
       
          Heii.. friends !! Hope you have gone through my previous posts regarding the basics in java . This post is the continuation of the previous post so if you haven’t gone through it, I recommend you to look at that before continuing with this..

WHAT IS A METHOD..??
    
          Hah.. hope you know about our boss main ( ) method . If you don’t know what a public static void main method does please refer the post class and object in java

STORY BEGINS..

          Consider our main( ) method is the boss of a company . He is the one who will take care of all the stuffs.. from accepting the requests till solving it. !! 

        At certain point, ,main ( ) method has turned old .. so he wasn’t able to deal with all the work he was supposed to do..

       So boss main ( ) has ended up with a decision … 

       That is .. having an assistant who’s other name is “method” .

       We all know what the assistant actually does.

       He will help the boss in completing the work. Similarly here too.. methods generally help the main ( ) method to complete the work.

    
   HOW THE METHOD HELPS THE MAIN ( ) METHOD ??



      Consider an example…
 
         Class bossy
           {
              public void addhelper( )
                   {
                      int sad = 5 ;
                      int happy = 10;
                      int sum = sad  +happy;
                      System.out.println( “ My num is “ + sum );
                    }
              public static void main(String args[])
                 {
                    bossy boss = new bossy ( );
                    boss.addhelper();
                 }
           }

     EXPLANATION:


      
          The class name is bossy and till this point we have created example without method but here a point has come to create a method as the boss main ( ) has turned old.

          So the method name is addhelper( ).

         The addition has to be done. So, the main method just asks the assistant method to do the work by calling it .

         “Hey addhelper..!! just do the addition work .. !! You got?? “ is what the boss demands..!!
And.. the method will perform addition and print the result..

         So, now the boss’s work has become easy..he has now got an assistant who could do all the work.

      CREATING ACTUAL OBJECTS …!!!
                But what’s the code  bossy boss = new bossy ( ); “ is actually doing there in above example??

          Well class “bossy” has everything. ie. The entire template but how it can be accessed ??
Here the object comes..!! 

           The object will actually help to access the variables and methods which in turn together are called as  “ Member functions “

  
      WHEN THE OBJECT COMES TO LIFE??



             Okies.. object is actually born when the keyword “new” is used.

             bossy boss = new bossy ( );

              Here  bossy is the name of the class and the reference name is “boss”

             Note it is just a reference till this point of time.

     USE OF NEW KEYWORD: 



        When the keyword “new” is used, the compiler will allocate the memory for the entire class which is holding the member functions.

        Consider the class is present in the address “1234”

        Now the object will contain the address “1234” . So the object will contain the address.

WHAT IS THE DIFFERENCE BETWEEN REFERENCE AND OBJECT??



 The word which is next to the class name is reference. Its just a reference of the class but…

  When the new keyword is used , it turns as an object.

  The “new” keyword is the one which is helping for memory allocation.

  Without this keyword ,objects can’t be created

   So, bossy boss = new bossy ( ); is the syntax for creating object for a class. Once object is created, using that object , members like variable and methods can be accessed as how we have done in above example.


   HOW PASSING PARAMETER WORKS??

       Ok.. now the boss is feeling pity on the assistant ie. method “addhelper “ .

       The boss thinks.. Oh pity guy is doing everything on its own.. let me help by giving the values atleast  is what the boss is thinking now.. 

Example now becomes..

       Class bossy
           {
               public void addhelper( int sad, int happy)
                   {
                     int sum = sad  +happy;
                     System.out.println( “ My num is “ + sum );
                   }
                 public static void main(String args[])
                    {
                     bossy boss = new bossy ( );
                     boss.addhelper(5,5);
                    }
               }
    EXPLANATION:

         So,here is how the boss is helping.. the boss is passing the values “5,5 “ while calling the method.

       Hey man.. take these values and perform the sum .. is what the boss is saying now!!

        But the method must be prepared with the data type . The data type of the values which the boss pass must match with the argument type which is passed in..

  DIFFERENCE BETWEEN PARAMETER AND ARGUMENT:



         The difference between Parameter and Argument is in short ,  the stuffs which is present in method ie.

      Addhelper(int a, int b) .Here int a,int b are the parameters

      And here addhelper(5, 5 ). Here 5, 5 are the arguments

        Now understood .. ? Hope your would have  :D 
 
   EXPECTATIONS ARE ENDLESSS…

             Now what’s again ??

                The assistant is never ever contended with his work. So, he has decided to satisfy the boss by not only performing the work but also showing the work to the boss . In short to make the boss feel happy and proud.

                 Retruning values back to the boss:

      EXAMPLE:

       Class bossy
         {
           public int addhelper( int sad, int happy) ------- > Not public void addhelper
              {
                  int sum = sad  +happy;
                  return sum;
             }
           public static void main(String args[])
            {
               bossy boss = new bossy ( );
               boss.addhelper(5,5);
                System.out.println( “ My num is “ + sum );
             }
         }

       EXPLANATION:

             Here in the above code, only one line of code has been added. Ie. “return sum”

             The method addhelper( ) has not only done his work correctly but has showed and returned his work to the boss just to make him feel contended.

           The value is returned and it is printed in the main method.

           Note the change of void to int In the above example.

           In the previous example the method was public void addhelper ( ) . The keyword is void because the method didn’t return anything. Ie. it didn’t show his work to his boss.

          But here the method has returned the value to the boss. Since the method is returning integer value ,the datatype is “int”.

CONCLUSION:

     So,that's it for this post .Please post your comments below,share with your  friends and hit Like button . Request for the post you wish is welcomed. I am sure you would have enjoyed reading the post. Have a pleasant day :)