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:56

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

查看更多
登录 后发表回答