What is polymorphic method in java?

后端 未结 5 1485
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 05:39

In the context of Java, please explain what a \"polymorphic method\" is.

5条回答
  •  天涯浪人
    2020-12-11 06:25

    Polymorphism is a process of representing 'one form in many forms'.

    It is not a programming concept but it is one of the principle.

    Example 1 :
    
    class A
    {
     void print(double d)
     {
      System.out.println("Inside Double");
     }
     void print(float f)
     {
      System.out.println("Inside Float");
     }
    }
    class B
    {
     public static void main(String [ ] args)
     {
      A obj1 = new A();
      obj1.print(10.0);
     }
    }
    
    
    Output :
    
    //save as : B.java
    //compile as :javac B.java
    //run as : java B
    
    Inside Double
    
    ______________________
    
    
    Example 2 :
    
    class A
    {
     void print(double d)
     {
      System.out.println("Inside Double");
     }
     void print(float f)
     {
      System.out.println("Inside Float");
     }
    }
    class B
    {
     public static void main(String [ ] args)
     {
      A obj1 = new A();
      obj1.print(10.0f);
     }
    }
    
    
    Output :
    
    //save as : B.java
    //compile as :javac B.java
    //run as : java B
    
    Inside Float
    
    _______________________
    
    Example 3 :
    
    class A
    {
     void print(double d)
     {
      System.out.println("Inside Double");
     }
     void print(float f)
     {
      System.out.println("Inside Float");
     }
    }
    class B
    {
     public static void main(String [ ] args)
     {
      A obj1 = new A();
      obj1.print(10);
     }
    }
    
    
    Output :
    
    //save as : B.java
    //compile as :javac B.java
    //run as : java B
    
    Inside Float
    

    To know more - http://algovalley.com/java/polymorphism.php

提交回复
热议问题