What is polymorphic method in java?

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

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

相关标签:
5条回答
  • 2020-12-11 06:20

    "Polymorphic" means "many shapes." In Java, you can have a superclass with subclasses that do different things, using the same name. The traditional example is superclass Shape, with subclasses Circle, Square, and Rectangle, and method area().

    So, for example

    // note code is abbreviated, this is just for explanation
    class Shape {
        public int area();  // no implementation, this is abstract
    }
    
    class Circle {
        private int radius;
        public Circle(int r){ radius = r ; }
        public int area(){ return Math.PI*radius*radius ; }
    }
    
    class Square {
        private int wid;
        Public Square(int w){ wid=w; }
        public int area() { return wid*wid; }
    }
    

    Now consider an example

    Shape s[] = new Shape[2];
    
    s[0] = new Circle(10);
    s[1] = new Square(10);
    
    System.out.println("Area of s[0] "+s[0].area());
    System.out.println("Area of s[1] "+s[1].area());
    

    s[0].area() calls Circle.area(), s[1].area() calls Square.area() -- and thus we say that Shape and its subclasses exploit polymorphic calls to the method area.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-11 06:26

    A polymorphic method is a method that can take many forms. By that that I mean, the method may at different times invoke different methods.

    Let's say you got a class Animal and a class Dog extends Animal and a class Cat extends Animal, and they all override the method sleep()

    Then..

    animal.sleep();
    

    ..can call different methods depending on the dynamic type stored in the variable animal

    0 讨论(0)
  • 2020-12-11 06:27

    A method is signature polymorphic if all of the following are true:

    It is declared in the java.lang.invoke.MethodHandle class.

    It has a single formal parameter of type Object[].

    It has a return type of Object.

    It has the ACC_VARARGS and ACC_NATIVE flags set.

    In Java SE 8, the only signature polymorphic methods are the invoke and invokeExact methods of the class java.lang.invoke.MethodHandle.

    JVM specification 2.9. Special Methods

    0 讨论(0)
  • 2020-12-11 06:32

    Charlie's answer explains in simple terms what polymorphism is.

    Continuing from there, this would be a "polymorphic method":

    public void Shape CreateShape() {
        return new Circle(10);
    }
    

    It's "polymorphic" in the sense that its signature says you 're getting a Shape, but what you are really getting is a subclass of Shape. Since you don't know exactly what you are getting (could be a Circle, a Square, etc), you have to handle it using the interface of the super class (i.e., polymorphism).

    I should mention that (possibly because I only have slight experience with Java) "polymorphic method" is an unfamiliar term so it might be used to mean something else. This is just my interpretation.

    0 讨论(0)
提交回复
热议问题