Polimorfismo é a propriedade que permite assumir varios comportamentos. Abaixo exemplo com os 3 tipos de polimorfismo:
package contestQuestions;
public interface Animal {
public String comunicacao();
int returnNum(int n);
}
package contestQuestions;
public class Cao implements Animal{
private String nome;
private int a;
public Cao(String n, int a) {
// TODO Auto-generated constructor stub
this.nome = n;
this.a = a;
}
@Override
public String comunicacao() {
// TODO Auto-generated method stub
return "caim caim";
}
public double returnNum(double d) {
// TODO Auto-generated method stub
return d;
}
@Override
public int returnNum(int n) {
// TODO Auto-generated method stub
return n;
}
}
package contestQuestions;
public class Felino implements Animal{
private String nome;
private int num;
private double doubleNum;
public Felino(String name, int n) {
// TODO Auto-generated constructor stub
this.nome = name;
this.num = n;
}
@Override
public String comunicacao() {
// TODO Auto-generated method stub
return "miau";
}
@Override
public int returnNum(int n) {
// TODO Auto-generated method stub
this.num = n;
return this.num;
}
public double returnNum(double d) {
// TODO Auto-generated method stub
this.doubleNum = d;
return this.doubleNum;
}
}
package contestQuestions;
import java.util.ArrayList;
import java.util.Collection;
import javax.accessibility.AccessibleEditableText;
public class Parametrism {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Felino f1 = new Felino("damnd",666);
Felino f2 = new Felino("dao",777);
Cao c1 = new Cao("fido",1);
f1.returnNum(85.7);
f2.returnNum(8);
c1.returnNum(666);
Collection animals = new ArrayList();
animals.add(f1);
animals.add(f2);
animals.add(c1);
//
for (Animal a : animals)
System.out.println(a.comunicacao());
for (Animal a : animals)
System.out.println(a.returnNum(666));
}
}