What is polymorphic method in java?
In the context of Java, please explain what a "polymorphic met开发者_如何学Chod" is.
"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'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.
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
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
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
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
精彩评论