What is polymorphic method in java?

笑着哭i 提交于 2020-01-09 11:11:44

问题


I'm studying java language for SCJP test.

It is little bit hard to understand "polymorphic method".

Could you explain it for me? or give me some links?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/4605669/what-is-polymorphic-method-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!