What is polymorphic method in java?

2019-01-19 18:08发布

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?

7条回答
走好不送
2楼-- · 2019-01-19 18:36

This answer assumes knowledge of overloading , inheritance and generics.

Any method where we can see the following polymorphism is termed as polymorphic method. Note that a method is uniquely recognised by it's signature only. Return type don't count. Hence , method polymorphism has nothing to do with it's return type.

Types of Polymorphism/Polymorphic Method

The word polymorphism has its root in two Greek words: poly (means many) and morphos (means form). In programming, polymorphism is the ability of an entity (e.g., variable, class, method, object, code, parameter, etc.) to take on different meanings in different contexts. The entity that takes on different meanings is known as a polymorphic entity. Various types of polymorphism exist. Each type of polymorphism has a name that usually indicates how that type of polymorphism is achieved in practice. The proper use of polymorphism results in generic and reusable code.

Polymorphism can be categorized in the following two categories:

  • Ad hoc polymorphism

    • Overloading polymorphism
    • Coercion polymorphism
  • Universal polymorphism

    • Inclusion polymorphism
    • Parametric Polymorphism

Let us discuss Ad hoc polymorphism first. If a piece of code works for a finite number of types and all those types must be known when the code is written, it is known as ad hoc polymorphism. Ad hoc polymorphism is also known as apparent polymorphism because it is not polymorphism in a true sense. Some computer science purists do not consider ad hoc polymorphism to be polymorphism at all.

Ad hoc polymorphism is further divided into two categories:

a) Overloading polymorphism

b) Coercion polymorphism

Overloading polymorphism :

Let us discuss first Overloading polymorphism. Overloading is an ad hoc polymorphism. Overloading results when a method (called a method in Java and a function in other languages) or an operator has at least two definitions that work on different types. In such cases, the same name (for the method or operator) is used for their different definitions. That is, the same name exhibits many behaviours and hence the polymorphism. Such methods and operators are called overloaded methods and overloaded operators. Java lets you define overloaded methods. Java has some overloaded operators (like string operator which with integer adds them but with string concatenates), but it does not let you overload an operator for an ADT. That is, you cannot provide a new definition for an operator in Java.

Hence all overloaded methods are Polymorphic.

Coercion Polymorphism

Coercion is an ad hoc polymorphism. Coercion occurs when a type is implicitly converted (coerced) to another type automatically even if it was not intended explicitly. Consider the following statements in Java:

 int num = 707;
 double d2 = num; //Coercion  - Implicit Type conversion

Let's see it in terms of a method :

double square(double num) {
    return num * num;
}

In this the square() method can be called by integer parameters or from double parameters. Don’t confuse this polymorphism with polymorphism of super type accepting subtypes. That is Inclusion polymorphism which comes under Universal polymorphism. The difference is that here one is not subtype of another. There is no inheritance.

Suppose m is a method that declares a formal parameter of type T. If S is a type that can be implicitly converted to T, the method m is said to be polymorphic with respect to S and T.

Now we discuss Universal Polymorphism. If a piece of code is written in such a way that it works for an infinite number of types (will also work for new types not known at the time the code is written), it is called universal polymorphism. In universal polymorphism, the same code works on many types, whereas in ad hoc polymorphism, different implementations of code are provided for different types, giving an apparent impression of polymorphism.

Inclusion Polymorphism

Inclusion is a universal polymorphism. It is also known as subtype (or subclass) polymorphism because it is achieved using subtyping or sub classing. This is the most common type of polymorphism supported by object-oriented programming languages. Java supports it. Inclusion polymorphism occurs when a piece of code that is written using a type works for all its subtypes.

Java supports inclusion polymorphism using inheritance, which is a sub classing mechanism. You can define a method in Java using a formal parameter of a type, for example, Person, and that method can be called on all its subtypes, for example, Employee, Student, Customer, etc. Suppose you have a method processDetails() as follows:

void processDetails(Person p) {
/* Write code using the formal parameter p, which is of type Person. The same code will
work if an object of any of the subclass of Person is passed to this method.
*/
}

A piece of code is called universally polymorphic only if it works on an infinite number of types. In the case of inclusion polymorphism, the number of types for which the code works is constrained but infinite. The constraint is that all types must be the subtype of the type in whose term the code is written. If there is no restriction on how many subtypes a type can have, the number of subtypes is infinite (at least in theory).

Suppose m is a method that declares a formal parameter of supertype T. If S is a subtype the method m is said to be polymorphic with respect to S and T.

Parametric Polymorphism

Parametric is a universal polymorphism. It is also called “true” polymorphism because it lets you write true generic code that works for any types (related or unrelated). Sometimes, it is also referred to as generics.

In parametric polymorphism, a piece of code is written in such a way that it works on any type. Contrast parametric polymorphism with inclusion polymorphism. In inclusion polymorphism, code is written for one type and it works for all of its subtypes. It means all types for which the code works in inclusion polymorphism are related by a supertype-subtype relationship. However, in parametric polymorphism, the same code works for all types, which are not necessarily related.

Parametric polymorphism is achieved by using a type variable when writing the code, rather than using any specific type. The type variable assumes a specific type for which the code needs to be executed. Java supports parametric polymorphism since Java 5 through generics. Java supports polymorphic entities (e.g., parameterized classes) as well as polymorphic methods (parameterized methods) that use parametric polymorphism. In Java, parametric polymorphism is achieved in using generics.

class Collections {
    public static <T, S extends T> void copy(List<T> dest, List<S> src) {
    ...
}

This concludes what is a polymorphic method in Java and its type.

Source

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-19 18:38

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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-19 18:46

polymorphic method in java

In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-19 18:47

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.

查看更多
萌系小妹纸
6楼-- · 2019-01-19 18:48

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

查看更多
相关推荐>>
7楼-- · 2019-01-19 18:52

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

查看更多
登录 后发表回答