JAVA

Polymorphism In Java

polymorphism in java
polymorphism in java

Polymorphism In Java

polymorphism in java
polymorphism in java

Polymorphism is the feature of Object Oriented Programming that identifies one-to-many processes. It means multiple functions and tasks can be executed with a similar identifier. The system binds the memory at program compilation time known as compile-time polymorphism, early binding, and static binding. The system also binds the data at program execution time that is known as run-time polymorphism, late binding, and dynamic binding. The function overloading, constructor overloading etc are compile-time polymorphism and function overriding are the example of run-time polymorphism.

 

Types of Polymorphism
Image Source:- https://tech.queryhome.com

 

Function Overloading

Function Overloading is the feature of polymorphism that allows performing multiple tasks with similar identifier but defines different parameters. It is basically used to manage multiple processing and access with the help of its parameter. It is the example of run-time polymorphism.

 

java method overloading1 - Polymorphism In Java
Image Source:- http://www.trustingeeks.com

 

Example:-
Class A
{
   void s(int a)
   {
      if(a>0)
      {
         System.out.print("value is positive");
         }
      else{
        System.out.print("value is negative");
           }
         }
      void s(int x, int y)
       {
          int z=x;
          int x=y;
          int y=z;
         System.out.print("x="+x+"y="+y);
          }
      void s()
      {
          System.out.print("Demo of FO");
           }
      void s(int a, int b, int c)
      {
         int d=a+b+c;
         System.out.print("sum is"+d);
          }
      void s(char c)
      {
         System.out.print("ASCII value is"+(int)c);
          }
      public static void main(String s[])
      {
         A z = new A();
         z.s();
         z.s(10,10);
         z.s(10);
         z.s(10,5,9);
         z.s('A');
        }
}

 

Constructor

A constructor is similar to function but the class name and function name are same. It does not define a return type and non-return type and it auto execute when we create an object of existing class. It is basically used to initialize the value as a particular variable.

java constructorsdefaultconstsyntax - Polymorphism In Java
Image Source:- http://www.sree9it.com

 

Example:-
Class A
{
     A()
     {
        Char c = 'A';
        System.out.print("ASCII value is"+(int)c);
         }
     public static void main (String s[])
     {
        A z = new A();
         }
}

 

Constructor Overloading

The constructor is used to initialize the value and process when we create an object of existing class. We can use more than one constructor in a class with different parameter similar to function overloading.

 

Example:-

Class koo

{

koo()

{

System.out.print(“Demo of constructor overloading”);

}

koo(int a, int b)

{

int c=a+b;

System.out.print(“sum is”+c);

}

koo (char c)

{

System.out.print(“ASCII value is”+(int)c);

}

public static void main (String s[])

{

koo k = new koo();

koo k1 = new koo(10);

koo k2 = new koo(5,9);

koo k3 = new koo(‘A’);

}

}

koo(int a)

{

if(a>0)

{

System.out.print(“value is positive”);

}

else {

System.out.print(“value is negative”);

}

}

About the author

Tutorial Circle

Add Comment

Click here to post a comment