Thursday 22 November 2012

POLYMORPHISM WITH STATIC METHODS



POLYMORPHISM WITH STATIC METHODS:
Method hiding:
Writing two or more static approaches with the same name and same signatures in extremely and sub sessions is known as technique hidding.(this i not overrinding)
A static technique is a technique whose individual duplicate in storage is distributed by all the things of the category. Fixed technique are part of the category rather then to the item. So they are also known as category techniques. When static techniques are overridden since they do not rely on the item the coffee compiler need not delay until the things are designed to comprehend which technique is known as.
Let us re-write the program 2 using static techniques to comprehend technique concealing idea.

PROGRAM 3
:
        Create a program to use extremely category referrals to contact the determine () technique.
//static polymorphism
class one
{
      //method to determine rectangle value
      static gap calculate(double x)
       {
            program.out.println("square value ="+(x*x));
        }
}
class two exends one
{
     //method to determine rectangle primary value
     static gap determine (double x)
     {
        program.out.println("square primary = "+math.sqrt(x));
      }
}
class poly
{
       community static gap main(string args[])
       {
              //super category referrals represents sub category object
               one o = new two();
              //call calculate() technique using extremely category reference
               o.calculate(25);
        }
}

Output:
c:\>javc poly.java
c:\>java poly
square value = 625.0


        In previous program the extremely category technique is known as. If sub category referrals is used to contact the determine  ()method then sub category technique id known as. In college poly we implemented. assume we make a sub category item as:
                   one o =new two ();
       Here 'o' is extremely category referrals varying this varying we are contacting the determine () technique as o.calculate (25). so the extremely category calculate() technique is implemented. assistance we make a sub category referrals as
                  two t = new two();
      And known as the determine () technique using this sub category referrals as t.calculate (25) then the sub category determine () is implemented by the JVM.

Note:- Method concealing idea is just like technique overriding but in technique concealing we are using two static techniques with the same and same parameter in the extremely and sub sessions.
      The technique overriding idea is identified by the JVM at playback.
      The technique concealing idea is identified by the compiler at compietime.

POLYMORPHISM WITH PRIVATE METHODS:
       Personal techniques are the techniques which are announced by using the accessibility specifier 'private'. This accessibility specifier creates the technique not to be available outside the category. So other developers cannot accessibility the individual techniques. Even private techniques are not available in sub sessions. this implies there is no probability to bypass the individual techniques of the extremely category in its sub sessions. So only technique over filling is possible in situation of personal techniques.

       What happens if a technique is revealed is the sub category with the same name as that of the individual technique of the extremely class? It is possible to do so. But in this situation the technique in the extremely category in its own duplicate and the sub category will have its own duplicate. It does not come under method overriding.

IMPORTANT INTERVIEW QUESTION:

Q.Can you bypass private methods?
          No private techniques are not available in the sub sessions so they cannot be overridden the only way to contact the individual techniques of a category is by contacting them within the category. For this objective we should make a online community technique and contact the individual technique from with in it. When this community technique is known as the individual technique.

POLYMORPHISM WITH FINAL METHODS
:
       Methods which are announced as 'final' are known as final techniques. Last technique cannot be overridden because they are not available to the sub sessions. Therefore only strategy over filling is possible with final techniques.

       When the developer does not want other to bypass his technique he should announce his technique as 'final'.

FINAL CLASS:
      Any category which is released as final. final keyword and key phrase before a category stops monetary gift. this implies sub sessions cannot be designed to a final category. For example
     Last category A
     category B expands A // incorrect     

No comments:

Post a Comment