What is polymorphic method in java?
Asked Answered
T

6

8

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

Tame answered 5/1, 2011 at 15:17 Comment(4)
Do you have trouble with the term (i.e. would paraphrasing help)? Or do you have trouble with the concept of polymorphism?Snippet
First do you understand polymorphism?Boggart
Sunoracle tutorial about polymorphism in JavaAware
Related question in this very siteHyacinthhyacintha
R
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.

Raber answered 5/1, 2011 at 15:25 Comment(1)
Question asks for polymorphic method and not what is polymorphism.Hillary
P
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.

Proximate answered 5/1, 2011 at 15:31 Comment(2)
This is not what polymorphic method is in Java. Look at my ansHillary
Where is your answer @Number945? Your link don't point to it. Could you explain what a phoymorphic method is in Java?Winterize
P
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

Potassium answered 5/6, 2018 at 18:21 Comment(0)
C
1

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

Claro answered 25/5, 2022 at 10:50 Comment(0)
K
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

Karolkarola answered 26/3, 2016 at 18:33 Comment(0)
V
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

Valladolid answered 9/6, 2016 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.