What is a polymorphic method in java?

I am learning the Java language for the SCJP test.

It's a little difficult to understand the "polymorphic method."

Could you explain this to me? or give me some links?

+2
source share
5 answers

"Polymorphic" means "many forms." In Java, you can have a superclass with subclasses that do different things using the same name. A traditional example is the Shape superclass with subclasses of Circle , Square and Rectangle and the area() method.

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 therefore we say that Shape and its subclasses use polymorphic calls in the method area.

+16
source

Charlie's answer explains in simple terms what polymorphism is.

Continuing from there, it will be a "polymorphic method":

 public void Shape CreateShape() { return new Circle(10); } 

It is "polymorphic" in the sense that its signature says that you get a Shape , but what you really get is a subclass of Shape . Since you do not know exactly what you are getting (maybe Circle , a Square , etc.), you should process it using the superclass interface (i.e. Polymorphism).

I should mention this (possibly because I only have a little experience with Java). The "polymorphic method" is an unfamiliar term, so it can be used to refer to something else. This is just my interpretation.

+5
source

Polymorphism is the process of representing "one form in many forms."

This is not a programming concept, but it is one of the principles.

 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 learn more - http://algovalley.com/java/polymorphism.php

0
source

A method is a polymorphic signature if all of the following conditions 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.

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 java.lang.invoke.MethodHandle class.

JVM Specification 2.9. Special methods

0
source

polymorphic method in java

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

0
source

All Articles