POLYMORPHISM

Polymorphism


  • It is a particular statement, which will give different output at different places of execution (OR) A form that gives multiple behavior.
  • It’s comes under dynamic binding.
  • There are two types of polymorphisms
       (1). Static polymorphism
       (2). Dynamic polymorphism
  • In java there is only one polymorphism that is dynamic or late binding / polymorphism. Method Overloading won’t come under polymorphism.
  • We can achieve polymorphism through method overriding.
  • Polymorphism is defined as overriding super class behavior inside sub class is called as “method overriding” (OR) “Polymorphism”.
  • Without inheritance we can’t achieve method overriding. Without method overriding we can’t achieve polymorphism. So Without inheritance we can’t achieve Polymorphism.
  

Method overriding Rules :


    Step 1 :   Method name should be same as that of super class method name.

    Step 2 :   Return type should be same as super class method incase of primitive . We can go for same type (OR)sub class type in case of Derived.

    Step 3 :   Method signature should be same.

    Step 4 :   Access level should be same as that of super class method. Access level (OR) wider than that, should not (the private) be narrow.

    Step 5 :   Definition should be different (OR) Body of the method should be Changed(OR) different.

    Step 6 :   In case of throws,super class may/may not be available while overriding. If we keep throws in sub class it should have same type of super class throws exception type (OR) sub class to that exception type and also order must be same.

Example 1:

 
						
                    class  A
                    {
                       void test()
                       {
                          System.out.println(“hi ram”);
                       }
                    }
                    class  B extends  A
                    {
                       /* here  we are tring to override test method which */
                       /* inherited from superclass ie. From A */
                       void test()
                       {
                          System.out.println(“hello ram”);
                       }
                    }
                    class  Hai
                    {
                       public static void main(String args[])
                       {
                          A  a1=new  A();
                          B  b1=new  B();
                          A  a2=new  B();
                          a1.test();
                          b1.test();
                          a2.test();
                       }
                    } 
                        

   Output:

hi ram hello ram hello ram

Explanation :

The above program consists two test( ) I.e. one is available under class A and another one is available under class B means we are overriding test( ).

Example 2:

 
						
                     class  A
                     {
                        int m1()
                        {
                           System.out.println(“int-A”);
                           return 10;
                        }
                        public void m2()
                        {
                           System.out.println(“m2-A”);
                        }
                        private void m3()
                        {
                           System.out.println(“m3-A”);
                        }
                        static public void m4() 
                        {
                           System.out.println(“m4-A”);  
                        }
                     }
                     class  B extends  A
                     {
                        public int m1()
                        {
                            System.out.println(“int-B”);
                            return 20;
                        }
                        void m2()
                        {
                           System.out.println(“m2-B”);
                        }
                        public void m3()
                        {
                           System.out.println(“m3-B”);               
                        }
                        public static void m4()
                        {
                              System.out.println(“m4-B”);                
                        }
                     }
                     class Manager
                     {
                        public static void main(String  args[])
                        {
                           A  a1=new  A();
                           B  b1=new  B(); 
                           A  a2=new  B()
                           a1.m1();
                           a1.m2();
                           a1.m3(); /* compilation fail  because of it is a private method. */
                           a1.m4();
                           b1.m1();
                           b1.m2();
                           b1.m3();
                           b1.m4();
                           a2.m1();
                           a2.m2();
                           /* a2.m3(); */
                           /* compilation fails because  it is a private method . */
                           /* It can’t be  inherited  to the sub class. */
                           A.m4();   
                         }
                     }                        

   Output:

int-A m2-A m4-A int-B m2-B m3-B m4-B int-B m2-B m4-B

Explanation :

The above program have different methods like m1( ),m2( ), m3( ), m4( ),which acts differently based on their calling mechanism. if m1( ) is called by using a1 object then it acts based on the behaviors of class A, if we call m1( ) by using b1 it acts based on the behaviors of class B .And the same manner is also applicable for the m2( ), m3( ), m4( ) methods .