8

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

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43
Joseph
  • 81
  • 1
  • 1
  • 2
  • Do you have trouble with the term (i.e. would paraphrasing help)? Or do you have trouble with the concept of polymorphism? –  Jan 05 '11 at 15:19
  • 1
    First do you understand polymorphism? – jzd Jan 05 '11 at 15:20
  • [Sunoracle tutorial about polymorphism in Java](http://download.oracle.com/javase/tutorial/java/IandI/polymorphism.html) – wkl Jan 05 '11 at 15:22
  • [Related question in this very site](http://stackoverflow.com/questions/1031273/what-is-polymorphism) – Gnoupi Jan 05 '11 at 15:43

6 Answers6

21

"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.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
7

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • This is not what polymorphic method is in Java. Look at my [ans](https://stackoverflow.com/a/49897215/2844702) – Number945 Apr 18 '18 at 10:20
  • Where is your answer @Number945? Your link don't point to it. Could you explain what a phoymorphic method is in Java? – Nil Suria Jan 09 '21 at 12:18
1

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

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0

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

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

A polymorphic method or function is a function (static) or method in java which has a generic return type.

For example:

class SomeCollection<T> {

   public <T> getFirstItem() //left unimplemented for example reasons
}

say for example, we instantiate a new SomeCollection<MyObject>();, this would mean that the method getFirstItem() would return a MyObject in this instance.

This is what a polymorphic function/method is.

It's the combination of generics and polymorphism in a context of types defined in a method or function.

The function has many forms, which can be defined at object construction. In my example, the function constructs with a MyObject return type.

The other answers of this question are examples of polymorphism in method/function parameters.

I hope this helps.

Thanks, Rhys

Rhys Bradbury
  • 1,699
  • 13
  • 24