Wednesday 21 November 2012

POLYMORPHISM

Polymorphism  came from the ancient term. 'poly' indicates many and morphism indicates kinds. The functionality to are available in different kinds is known as 'polymorphism'. In coffee a varying an item or a technique can are available in different kinds, thus doing various projects based on the perspective. Because same varying or technique is capable of doing different projects. The primary benefits of polymorphism is to offer the versatility.
       There are two kinds of polymorphisms in coffee.
       (i) Fixed polymorphism (or) gather time polymorphism (or) beginning binding
       (ii) Powerful polymorphism (or) Playback polymorphism (or) dynamic binding

       STATIC POLYMORPHISM
:- The polymorphism displayed at collection time is known as static polymorphism. Here the coffee compiler knows without any indecisiveness which technique is known as at enough duration of collection. of course, JVM finishes the technique later, but the compiler known and can situation the technique contact with technique (body) at duration of collection. So, it is also known as 'static binding' or 'compile time polymorphsim'.

      EX:-To accomplish technique over filling by using static technique personal techniques and last techniques are illustrations for static polymorphism. Because these techniques will be identified at the gather time by the compiler.
  
      If we offer morethan one personal technique with same name & different parameter record is known as as last technique over filling and it will be identified at enough duration of obtaining.

      If we offer morethan one personal technique with same name & different parameter record is known as as personal technique over filling and it will be identified at enough duration of obtaining.

DYNAMIC POLYMORPHISM
:
       The polymorphism  displayed at runtime is known as as dynamic polymorphism.
 EX:
        Non personal.non static.non last technique over filling and The technique over filling principles are the best illustrations for Powerful Polymorphism.
 Note:- non personal,non last techniques will be recognized at the run time by the jvm just before doing the specific category constructor.These technique over filling is an example of Powerful Polymorphism due to above purpose.
IMPORTANT  INTERVIEW  QUESTION:
Q. What is a technique signature?
             Method trademark symbolizes the technique name along with the technique factors.When there is a distinction in the technique signatures, then the JVM is aware of both the techniques are different and can contact  the appropriate technique.The distinction in the technique signatures will occur because one of the following factors.
There may be a distinction in the variety of factors approved to the techniques.
For example,
            gap add(int a, int b)
            gap add(int a,int b,int c)
      In this situation, if we contact add(10,15) then JVM completes the first technique and if we contact add(10, 15, 22) then it operates the second technique.
Or , there may be a distinction in the information kinds of factors.
Program 1:
          Make a program to develop Examples category which contains two techniques with the same name but with various trademark.
//dynamic polymorphism
            category Sample
            {
                   //method to add two values
                     gap add(int a, int b)
                     {
                              Program.out.println("Sum of three="+(a+b));
                      }
                     //method to add three values
                      gap add(int a , int b , int c)
                      {
                              Program.out.println("Sum of three="+(a+b+c));
                      }
            }
           category Poly
           {
                    community static gap main(String args[])
                   {
                           //create example category object
                           Sample s = new Sample();
                           //call add() and complete two values
                           s.add(10,15);//This contact is limited with first method
                           //call add() and complete three values
                          s.add(10,15,22);//This contact is limited with second method
                   }
           }
Output:
C:/javac Poly.java
C:/java Poly
Sum of two = 25
Sum of three = 45

In the above program we did technique over filling using example methods(non static).And hence, Java compiler does not know which technique is known as at the collection time.But JVM knows and holds the technique contact with the appropriate technique at enough duration of operating the program.So, this is an example for dynamic polymorphism.

IMPORTANT INTERVIEW QUESTION
:

Q.What is technique over filling ?
           Composing two or more techniques in the same category in such a way that each technique has same name but with different  technique signatures-is known as technique over filling.
In program 1, we did technique over filling using example methods(non static). And hence, coffee compiler does not know which technique is known as at collection time. but JVM knowns and holds the technique contact with the appropriate technique at enough duration of operating the program. So, this is an example for dynamic polymorphism.

             We recognized that in technique over filling JVM identifies techniques independently by monitoring the different in the technique signatures that occurs due to the different either in the variety of factors or in the information kinds of factors or in the series of factors. It is not possible if the development wants to develop two techniques in the same category without any of these variations. Doing so will come under writing copy techniques and coffee compiler will decline that value.

             But it is always possible if the developer to develop two or more techniques with same name and same trademark in two different sessions. This can that done in extremely and sub sessions and this idea is known as as technique over filling.

IMPORTANT INTERVIEW QUESTION:

Q.What is technique overriding ?

           Composing two or more techniques in extremely and sub sessions. this can that the techniques have same name and same trademark - is known as technique overriding.
  
           In technique overriding the coffee compiler does not choose which technique is known as by the customer since it has to delay until an item to sub category is designed. After developing the item JVM has to situation the technique contact to an appropriated technique. but the techniques in extremely and sub sessions have same name and same technique signatures. then how JVM chooses which technique is called?

           See program 2. In this program the extremely category 'one' has determine () technique which determine rectangle value. The sub category two is resulting from category one. But the developer who is developing the sub category does not want to identify rectangle value. His need is to determine rectangle primary value. So he creates another technique with the same name and same trademark in the sub category but with a different body system i.e to determine the rectangle primary value.

Observe the techniques in the extremely and sub sessions published as.
           gap determine (double x)     // in extremely category one
           gap determine (double x)    //  in sub category two

           These two techniques have same name and same signatures and there is no variations any where in the factors. When determine () technique is known as by using the sub category item 't' as:
           t.calculate (25)

           The sub category technique is only implemented by the JVM but not the extremely category calculate() technique. This idea is also known as 'method overriding'.

PROGRAM 2:

            Make a program where determine () technique of extremely category is overridden by the determine () technique of sub category. The actions of the determine () technique is dynamically decided(method overriding)

//Dynamic polymorphism
class one
{
 //method to determine rectangle value
void category determine (double x)
 {
   program.out.println("square value =+(x*x));
   }
  }
class two expands one  
{
 //method to determine rectangle primary value
void calculate(double x)
  {
    program.out.println("sqare primary = "+math.sqrt(x));
   }
class poly
{
  community static gap primary (string args[])
  {
    //create sub gap category item t.
     two t= new two();
    //call calculate() technique using t.
     t.calculate(25);
   }
}
OUTPUT:
c:\>javac poly.java
c:\>java poly
square primary = 5.0

          Keep in mind when a extremely category technique is overridden by the sub category technique JVM phone calls only the sub category technique and never the sup[er category technique. In other terms we can say the sub category technique is changing the extremely category method

No comments:

Post a Comment