JAVA POLYMORPHISM

 

 

Polymorphism  in  java  is  one  of  the important  concept   which  allows  objects  to  behave  differently  based  on  their  specific  class  type .  In  Java , polymorphism  allows  the  same  object  or  method  to  behave  differently  based  on  the  context  of  the  project.

 

 

 

 

 

Features  of  Polymorphism:

 

  • Runtime  Decision :  Java  determines  which  method  to  call  depending  on  the  objects  actual  class  at  Runtime .
  • Multiple  Behaviors:   In  Java  Polymorphism ,  the same  method  can  behave  differently  depending  on  the   object .
  • Method Overriding :   A  subclass/childclass  can   redefine  a  method  of  its  superclass/parentclass  by  overriding  the  method  of  its  parent  class .
  • Method  Overloading : In  Java, we  can  define  multiple  methods  with  same  name  but  with   different  parameters  which  is  called  method  overloading.

 

 

 

 

 

 

Why  to  use  Polymorphism  In  Java?

 

Polymophism  in  Java  has  many  advantages  which are  listed  below :

 

  • Code  Reusability :   Polymorphism  allows  the  same  class  or  method  to  use  with  different  types  of   objects , which  makes  the  code  more  reusable .
  • Dynamic  Behavior :  In  Java , with  polymorphism , Java  can  select   the  appropriate  method to  call  at  runtime , which  gives  the  program  dynamic  behavior  based  on  the  actual  object  type  rather  than  the  reference  type , due  to  which  the  flexibility  of  the  program  increases .
  • Flexibility :  Java  Polymorphism  enables  the  object  of  the  classes  to  be  treated  as  objects  of  a  common  superclass ,  which  provides  flexibility  in  method  execution  and  object  interaction .
  • Abstraction : Java  Polymorphism    allows  the  use  of  abstract  classes  or  interfaces ,  enabling   us  to  work  with   superclass  or  interface  which  simplifies  the  interaction   with  objects .

 

 

 

 

 

 

 

TYPES   OF   JAVA   POLYMORPHISM :

 

In  Java , polymorphism  is  mainly  divided  into  two  types .

 

 

JAVA POLYMORPHISM

 

 

  1.  Compile-Time  Polymorphism :   Method  Overloading  in  Java  is  known  as  Compile-Time  polymorphism.   Compile-Time  Polymorphism  is  also  known  as   static  binding  or  early  binding.  Method  overloading  happens  when   multiple  methods   in  the  same  class  have   the  same  name  but  with  different  parameters .

 

 

 

Method  Overloading : 

 

 

Method  overloading  in  Java  means  when  there  are  multiple  methods  with  same  name  but  with   different  parameters .  The  number  of  arguments  can  be  different  in  different  method  or  types  of  arguments  can  be  different  in  method  overloading . But  only  change  in  return  types  cannot  make  the  method  overload  as  ambiguity  problem  may  occur .

 

 

 

Example :

 

class   Addition 

   {

       int  add  (  int a , int b )

       {

       int  sum= a+b;

       return  sum ;

        }

 

    int  add  ( int  a , int b , int  c )

     {

      int  sum =  a+b+c;

       return  sum;

     }

}

 

 

class      MethodoverloadingExample

{

        public   static  void main (String  args [] ) {

    Addition   aobj=new  Addition () ;

   System.out.println(aobj.add(34,28));

   System.out.println(aobj.add(29,35,1));

 

      }

   }

 

 

 

 

 OUTPUT :

62

65

 

2.  Runtime  Polymorphism:

 

 

 

Runtime  polymorphism  in  Java  is  also  known  as  Dynamic  Method  Dispatch or  Late  Binding . Java  Runtime  polymorphism  is  a  process  in  which  a  function  call  to  the  overriden  method  is  resolved  at  Runtime . This  Runtime  polymorphism  is  achieved  through  Method  overriding .

 

 

 

 

Method  Overriding

 

 

In  Java, Method  Overriding  means  when  a  subclass  method  provides  a  specific  implementation  of  a  method    which  is  already  present  in its  superclass.  The  overriding  method  in  the  subclass  must  have  the  same  name , return  type , and  parameters  as  that  of  the  superclass . Method  overriding  in  Java  allows  a  subclass  to  extend  or  modify  the  behavior  of  an  existing  method  of  the  super class .  Method  overriding  enables  dynamic  method  dispatch ,  where  the  method  that  gets  executed   is  determined  at  runtime  .

 

 

 

 

 

Example:

 

 

 

 

// Parent  class

 

class   Gift  {

 

   //Method  in  the  parent  class

 public  void   gift ( ) {

    System.out.println(“Gift  is  awesome “);

         }

 

     }

 

 

 

//Child  class  that  inherits   from  superclass  Gift

 

class   StudentGift  extends  Gift 

{

     @Override

      public  void  gift() {

            System.out.println(“Student  Gift  is an  incredible  book”);

              }

      }

 

//Child  class  that  inherits  from  superclass

 

 

   class   TeacherGift  extends  Gift

{

     @Override

       public  void  gift () {

        System.out.println(“Teachers  gift  is  $11000”);

       }

     }

 

 

//Main  class

 

public  class  MethodoverridingDemo{

   

  public  static  void  main (String  args [] ) {

      

// create  a  Gift  object

  Gift  gif =  new  Gift  () ;

    gif. gift();  //calls  the  Gift  class  method

 

//create  a  TeacherGift object

TeacherGift   tgif =new  TeacgerGift();

tgif.gift();          // calls  the  overriden  method  of  TeacherGift  class

 

//Achieve Dynamic  Dispatch or   Runtime  polymorphism

 

Gift   dgif= new     StudentGift();

dgif.gift();     // calls  the  overriden  method  in  StudentGift  class

 

 

     

 

 

 

 

 

OUTPUT:

 

Gift  is awesome

Teachers  gift  is  $11000

Student  gift  is   an  incredible  book

 

 

 

 

 

 

 

 

 

Method  Overloading  vs  Method  Overriding

 

The  difference  between  Method  Overloading  and  Method  Overriding  in  Java  are  as  follows:

 

 

 

 

 

FeatureMethod OverloadingMethod Overriding
DefinitionMethod overloading describes defining multiple methods with the same name but different parameters in the same classMethod Overriding means implementing a parent class method in the subclass with the same signature and compatible return type.
PurposeMethod Overloading in Java is used to achieve compile-time polymorphism which is also known as static binding.Method overriding in java is used to achieve runtime polymorphism which is also known as dynamic binding.
Parameter ListIn Method Overloading the parameter list must be different (in number , type or order )In Method Overriding, the parameter must be exactly the same as in the parent class.
InheritenceIn Java method overloading , inheritance is not required as overloading occurs within the same class.Java method overriding requires inheritance between super class and sub class.
Return typeReturn type of java method can be same or differentThe return data type of overriding method must be same or covariant type(sub type of superclass method’s return type )
Access ModifierJava method overloading can have any access modifierJava method overriding cannot reduce parent method’s access level.
Static/Final MethodsJava static/final methods can be overloadedJava static/final methods cannot be overriden.
Exception HandlingJava overloaded method can declare any exceptionsJava overriden method cannot throw broader checked exceptrions than the super class.
Binding TimeMethod overloading indicates Compile-time bindingMethod overriding indicates Run-time binding.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *