Skip to main content

Chapter - 8 Polymorphism in Java in Hindi and English

 Polymorphism in Java 

Definition :- It means that One name and more then forms that is called polymorphism, let's see an example A Programmer (You), and for another persons You are friend of friends and a Boyfriend of GF, Son of Your Father/Mother etc. So Your role is different - different platform. Read more.....

Post-Intro:- In this post we will learn what is Polymorphism why is important where is used and how to work in java with an example so let's go start now.


Table Of Content:

Java Polymorphism in Hindi 
Java Polymorphism in English
Types of Polymorphism in Java 



Java Polymorphism in Hindi


Java Polymorphism in English

  • It means one name many forms so we can say that in this type of programming same function is used to perform different kind of operation.
  • It is an important part of object oriented programming language.
  • The term of "Polymorphism" is the combination of "Poly" +"morphs" which means many forms. Actually it is a Greek  Word.
  • In OOP, we use 3 main concept in Inheritance, Encapsulation and Polymorphism.
  • A man behaves like a leader in our office and in home a father and son with a customer in market, so here a single person is behaving differently according to the situations. 
  • Let's See an Example of Polymorphism.

Example :

  • Let's take a simple example in which as you can see that class name is poly and there are four function with same name a() with different parameter so execution of function is based on the value passing at the time of calling.

class Poly{
  void a()
  {
   System.out.println("No parameterize");
  }
  void a(int i)
  {
   System.out.println("Integer parameterize");
  }
  void a(char ch)
  {
   System.out.println("Character parameterize");
  }
  void a(float f)
  {
   System.out.println("Float parameterize");
  }
  void a(double d)
  {
   System.out.println("Double parameterize");
  }
  void a(String s)
  {
   System.out.println("String parameterize");
  }
}
class DemoPoly{
 public static void main(String[] args)
 {
   Poly obj=new Poly();
  obj.a(12);
  obj.a('e');
  obj.a("DemoPoly");
 }
}


Example Explanation :

  • Here function is called three times first time with integer value and the second time with character value and third time with string value so the output is: 

Output :  


Integer parameterize
Character parameterize
String parameterize


Types of Polymorphism 

  • There are two types of polymorphism compile time and run-time polymorphism. so Java support both types of polymorphism.

1.Compile time Polymorphism with Example

    A. Function Overloading  

    B. Operator Overloading

2.Runtime Polymorphism

    A. Function Overriding

    1. Compile time Polymorphism

    • It is already known where is mistake before run (spelling, comma) which means that function will run compile time so it called compile time polymorphism and also called early binding. 
    • Early banding means that you are already bound to the function call and you known that this function is going to run.
    • Let's see an example of Compile time Polymorphism.
    • There are two types Function Overloading and Operator Overloading.

    A. Function Overloading 

    • The function with same name and different parameter is called function overloading

    // Example 1: Find the Geometry Formula
    class Geometry
    {
      void area(int height,int width)
      {
       int ar=height*width;
       System.out.println("Area of Rectangle="+ar);
      }
      void area(int side)
      {
       int ar=side*side;
       System.out.println("Area of Square="+ar);
      }
      void area(float r)
      {
       float ar=3.14f*r*r;
       System.out.println("Area of Circle="+ar);
      }
      void area(float base,float height)
      {
       float ar=0.5f*base*height;
       System.out.println("Area of Triangle="+ar);
      }
    }
    class Easy
    {
     public static void main(String[] args)
     {
      //Creating instance(object)
      Geometry obj=new Geometry();
      //single para for square
      obj.area(12);
      //double int para for rectangle
      obj.area(5,6);
      //single float for circle
      obj.area(2.2f);
      //double float for trianlge
      obj.area(2.5f,6.3f);
     }
    }

    Example Explanation :

    Here Geometry is class name contains two function with same name area() with different paramater,first with two parameter height and width and second with single parameter side .Therefor when two integer parameter is passed at the time of calling Rectangle area will be calculated, when single integer parameter is passed Square area will be calculated

    Output :  


    Area of Square=144 Area of Rectangle=30 Area of Circle=15.197601 Area of Triangle=7.875

    Example 2: Find Integers and Characters with Function Overloading


    class Overload{
      public static int sum(int a, int b){
        return a+b;
      }
      public static char sum(char s, char j){
        return s+j;
      }
      public static void main(String args[]){
        // System.out.println(sum("Shailesh", "Priya"));  
        // Can't Sum because string cannot be converted to int
        System.out.println(sum(4, 5));          
      }
    }



    B. Operator Overloading

    • Operator overloading is used to overload or redefines most of the operator available in Java.
    • For Example:
    • We can overload the operator "+" and defines its functionality to add two strings.
    • Operator loading is also an example of compile time polymorphism because the compiler already known at the compile time which operator is perform the task.
    • Operator Overloading Cannot overload are following : - 
      • Scope Operator (::) 
      • Sizeof()
      • member selector(.)
      • member pointer selector(*)
      • Ternary Operator(? ;)

    Example of Operator Overloading




    2. Runtime Polymorphism :

    • Function with same name and same parameter is called function overriding.
    • It is not possible to make two function with same name and same parameter in a single class therefor to implement function overriding derived class is used. 
    • Let's see an example of Function Overriding 

    A. Function Overriding  



      //Function Overriding

      class Geometry1
      {
        void area(int height,int width)
        {
         int ar=height*width;
         System.out.println("Area of Rectangle1="+ar);
        }
      }
      class Geometry2 extends Geometry1
      {
        void area(int height,int width)
        {
         int ar=height*width;
         System.out.println("Area of Rectangle2="+ar);
        }
      }

      class Easy
      {
       public static void main(String[] args)
       {
        //Creating instance(object)
        Geometry2 obj=new Geometry2();
        //calling function
        obj.area(12,13);
       }
      }


      Outputs : 

      Area of Rectangle2=156   







      Comments

      Popular posts from this blog

      Assignment of ITA/ Information Technology and Application BCA- Technology369kk

      Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

      C++ and Java Practical All Questions Answers - BCA -Technology369kk

      C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

      Assignment of PMO (Principal of Management and Organization) - Technology369kk

       ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...