Sunday, 1 November 2015

WHAT IS THE DIFFERENCE BETWEEN METHOD OVERLOADING AND METHOD OVERRIDING..?




    INTRODUCTION:

         Helooooo folks.. In this tutorial I am going to explain about method overriding and difference between method overloading and method overriding

   WHAT METHOD OVERRRIDING ACTUALLY IS..?

        Method overriding actually comes into life when the inheritance concepts comes into being..!! If you don’t know what inheritance actually mean ..please refer here for the detailed post Inheritance in java
    WHAT DOES A METHOD ACTUALLY RIDES..?

        When a  sub class actually contain same return type, same parameters, same  name etc then that method is said to override the SUPER CLASS method..

      Ok ..I don’t want to confuse more with technical terms..let me explain with an example..

    Class Dad
       {
           String color = “white”;
           String haricolor= “black”;
           public void Smartness( )
            {
               System.out.println(“ I am dad and I am smart ;) “);
            }
           public void Diabetes( )
             {
                  System.out.println(“ Ooops.. I am dad and I have diabetes” );
             }
       }

    Class Son extends Dad
      {
          String Height = “tall”;
          public void Smartness( )
           {
                System.out.println(“ I am dad and I am smart ;) “);
           }
          public void swimming ( )
           {
               System.out.printlln(“Hieeee..I am the son and I know swimming “);
          }
       public static void main(String args[])
         {
             Son Sonobj = new Son ( ) ;
             Sonobj.swimming( );
             Sonobj.Smartness( );
         }
   }



 OUTPUT:

      Hieeee..I am the son and I know swimming
      I am SON and I am smart ;)

  EXPLANATION:

          Here dad is the Super class and Son is the sub class.. You can see.. the sub class SON is having the same method as the super class  and when we call the Smartness method then it calls the method in the Son class and not the Dad class. 

        This is because “SMARTNESS method has overridden the method in the super class” and so this method is called.. !!

        But what if we want to call the SMARTNESS method in the SUPER CLASS ?

     IS THERE ANY WAY TO CALL THE SMARTNESS METHOD IN SUPER CLASS??

             Of course it’s a big ssssss !! We can call the method in the super class using the keyword “SUPER” 

       Wow.. its really SUPER.. !! nah ;) 

      HOW TO USE SUPER KEYWORD..?

       Considering the same example as above.. but extra only one keyword is added.. Look at  the Smartness method in the Son class..

     Class Dad
        {
          String color = “white”;
          String haricolor= “black”;
          public void Smartness( )
           {
               System.out.println(“ I am dad and I am smart ;) “);
           }
          public void Diabetes( )
           {
              System.out.println(“ Ooops.. I am dad and I have diabetes” );
          }
   }

      Class Son extends Dad
      {
         String Height = “tall”;
         public void Smartness( )
          {
             System.out.println(“ I am SON and I am smart ;) “);
             Super.Smartness( );
           }

          public void swimming ( )
           {
              System.out.printlln(“Hieeee..I am the son and I know swimming “);
           }
        public static void main(String args[])
           {
                 Son Sonobj = new Son ( ) ;
                 Sonobj.swimming( );
                 Sonobj.Smartness( );
          }
   }

  OUTPUT:

       Hieeee..I am the son and I know swimming
       I am SON and I am smart ;)
       I am dad and I am smart ;)

 EXPLANATION:

         Look at the example above..by using “SUPER.SMARTNESS( ) “ .. the smartness( ) method of the DAD class is called and printed.

       So only way of accessing the super class method if the method is overridden ie. if the same method is present in the sub class with the same type .. etc then it can be done using SUPER KEYWORD.

     CONCLUSION:

So that’s it for this post. Hope you understood the concepts better.. !! Hit like button ..share the post..drop your comments below..It will help me to post more !!  Suggestions and request for post you wish is welcomed.. Have a peaceful day :)

WHAT IS AN INHERITANCE IN JAVA..??




INTRODUCTION:

       Hey folks..in this post I will tell you what inheritance actually mean..!!Lets jump to the post..

INHERITANCE… A SIMPLE RELATIONSHIP

      Consider X’s dad as an example.. X’s dad will have properties like

           color – white
           hair- black etc and actions like,
           Smartness
           Diabetes

     So, the structure will be
     Class Dad
         {
            String color = “white”;
            String haricolor= “black”;
            public void Smartness( )
               {
                 System.out.println(“ I am dad and I am smart ;) “);
               }
           public void Diabetes( )
             {
                System.out.println(“ Ooops.. I am dad and I have diabetes” );
             }
      }

      Now look at the above example..

        X’s dad has properties like haircolor black and tone color as white , Actions like Smartness and Diabetes etc.

         X’s Dad is soo happy when X was born . He saw his son’s unique characteristics like..” tall “ and he was even more surprised when X realized that his son has his properties and actions ..

       He want to express this to his relatives that his son has his properties and actions inherited..
For that.. X’s dad wrote a structure and it was as follows..

       Class Dad
      {
          String color = “white”;
         String haricolor= “black”;
         public void Smartness( )
            {
                 System.out.println(“ I am dad and I am smart ;) “);
            }
         public void Diabetes( )
          {
               System.out.println(“ Ooops.. I am dad and I have diabetes” );
           }
      }
      Class Son extends Dad
        {
          String Height = “tall”
          public void swimming ( )
           {
             System.out.printlln(“Hieeee..I am the son and I know swimming “);
           }
      }


    EXPLANATION:

        Look at the above example here.. Class Dad is the super Class which means it is the top class and class Son is the sub class meaning this class is inheriting the properties of the super class. 

        So, the Class son has properties like height and the son does swimming..

       Ok fine .. but where is the dad specifying that the Son is inheriting his properties ??

        This is done in the SON SUBCLASS..

         Extends SUPERCLASS NAME  ( DAD) ------ >>> “is what telling everybody that my son is extending my characteristics , behavior, actions and in addition my son is having some unique characteristics and those will be listed under his class (Son Class) ..

         So, this is what INHERITANCE IS.. If we want a class to have same methods and variables of another class and in addition you want some unique methods and variables for the sub class then you can extend super class so that the sub class will contain all the variables , methods and functions of the super class and in addition will contain its own methods, variables etc.


   HOW TO INVOKE A MEMBER FUNCTION OF A SUB CLASS..? 

       It can be invoked by creating object for the sub class.

    DO WE NEED TO CREATE SEPARATE OBJECT FOR THE SUPER CLASS..??

        Definitely not.. !! We are supposed to create object for the sub class and using that sub class object even the super class variables and methods can be accessed.

    Example:

      Class Dad
      {
          String color = “white”;
         String haricolor= “black”;
         public void Smartness( )
             {
                System.out.println(“ I am dad and I am smart ;) “);
             }
        public void Diabetes( )
            {
                System.out.println(“ Ooops.. I am dad and I have diabetes” );
             }
       }

       Class Son extends Dad
        {
          String Height = “tall”;
          public void swimming ( )
               {
                   System.out.printlln(“Hieeee..I am the son and I know swimming “);
                }
         public static void main(String args[])
              {
                  Son Sonobj = new Son ( ) ;
                  Sonobj.swimming( );
                  Sonobj.Smartness( );
             }
         }

     OUTPUT:

        Hieeee..I am the son and I know swimming
        I am dad and I am smart ;)

   EXPLANATION:

         Look at the example.. we are creating object for the sub class.. we are calling a method “swimming” and using that same object we are calling the method of the SUPER CLASS“Smartness” . 

          This is because.. the SON sub class is inheriting all the methods and variables of the super class so, when we create the object for the sub class , it will have access to all the member functions of the DAD class.

        SON has got inherited characteristics and also some unique characteristics..

   WILL THE CODE RUN IF WE CREATE OBJECT FOR THE SUPER CLASS..??

                 Of course yes.. !! We can create separate object for the super class and invoke its member functions(variables and methods ) or we can create an object for the sub class and can invoke from that.!!

        SO WHY DO WE CREATE OBJECT FOR THE SUB CLASS AND CALL THE SUPER CLASS USING THAT OBJECT??

        The answer is pretty simple.. !! Its just efficiency !! When we create separate object for the super class then separate MEMORY ALLOCATION will be done and when we create separate object for the sub class then another memory also will get allocated for that object..

      Its just unnecessary waste of memory !!
 
       Pity machine has to handle everything.. let’s help it by avoiding this unnecessary memory allocation..!!

  CONCLUSION:

        So.. that’s it about this post.. !! Email me for any queries..Please hit LIKE button…drop your valuable feedbacks and help me post more..!! Request for the post  you wish and suggestions for the post is also welcomed.. !!
Stay tuned for more posts!! Have a pleasant day :)