Sunday, 23 October 2016

Compile time

Compile time polymorphism


Compile time polymorphism or static polymorphism
  • This polymorphism is used to happen at compile time.
  • The best example of compile time polymorphism is method overloading
Method overloading
  • In a class one or more methods having same name and different type of parameters or arguments.
Arguments may differ in overloading as follows 
  1. Number of parameters or count of parameters
  2. Type of parameters
  3. Order of parameters
1. Number of parameters
  • In a class one or more methods having same name and number of parameters are different.
  • Below method1 in a class Demo1 having polymorphic nature.
  • All method names same but number of arguments are different.
Rules for overloading
  1. Overloading can happen in same class or in its sub class.
  2. Even in any sub class also overloading concept can exists.
  3. Overloading concept not only for methods it will applicable for constructors also.
  4. In overloading methods have different arguments.
  5. return type is not part of overloading, so it can be same or different.
  6. Parameters of the arguments are different in terms of, number of parameters, type of parameters and order of parameters.
  7. Another name for overloading is compile time polymorphism.
---------------------------------------------------------------------------------------------------
Program           :       Overloading based on number of parameters
Program name  :       Overloading1.java
Output             :      
      method1 have no parameter
      method1 have one parameter
      method1 have two parameters

class Demo1
{
        public void method1()
        {
                System.out.println(“method1 have no parameter”);
        }
       
        public void method1(int i)
        {
                System.out.println(“method1 have one parameter”);
        }
       
        public void method1(int i, int j)
        {
                System.out.println(“method1 have two parameters”);
        }     
}

class Overloading1
{
        public static void main(String args[])
        {
                Demo1 d = new Demo1();

                d.method1();
                d.method1(34);
                d.method1(76,88);
        }
}

Compile           :       javac Overloading1.java
Run                 :       java Overloading1
Output             :      
     method1 have no parameter
     method1 have one parameter
     method1 have two parameters
---------------------------------------------------------------------------------------------------
2. Type of parameters
  • In a class one or more methods having same name and type of parameters are different.
  • Below method1 in a class Demo1 having polymorphic nature.
  • All method names same but type of arguments are different.
---------------------------------------------------------------------------------------------------
Program           :       Overloading based on type of parameters
Program name  :       Overloading2.java
Output             :      
      method1 have int parameter
      method1 have String parameter
      method1 have float parameters

class Demo2
{
        public void method1(int i)
        {
                System.out.println(“method1 have int parameter”);
        }
       
        public void method1(String s)
        {
                System.out.println(“method1 have String parameter”);
        }
       
        public void method1(float j)
        {
                System.out.println(“method1 have float parameter”);
        }     
}

class Overloading2
{
        public static void main(String args[])
        {
                Demo2 d = new Demo2();

                d.method1(12);
                d.method1("Java");
                d.method1(76.5f);
        }
}

Compile           :       javac Overloading2.java
Run                 :       java Overloading2
Output             :      
     method1 have int parameter
     method1 have String parameter
     method1 have float parameters
---------------------------------------------------------------------------------------------------
3. order of parameters
  • In a class one or more methods having same name and order of parameters are different.
  • Below method1 in a class Demo1 having polymorphic nature.
  • All method names same but order of arguments are different.
---------------------------------------------------------------------------------------------------
Program           :       Overloading based on order of parameters
Program name  :       Overloading3.java
Output             :      
      method1 have int and String parameter
      method1 have String and int parameter

class Demo3
{
        public void method1(int id1, String name1)
        {
                System.out.println(“method1 have int and String parameter”);
        }
       
        public void method1(String name2, int id2)
        {
                System.out.println(“method1 have String and int parameter”);
        }  
}

class Overloading3
{
        public static void main(String args[])
        {
                Demo3 d = new Demo3();

                d.method1(12, "Java");
                d.method1("Java", 23);
        }
}

Compile           :       javac Overloading3.java
Run                 :       java Overloading3
Output             :      
     method1 have int and String parameter
     method1 have String and int parameter
     
---------------------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan