You can pass an object and then check its type.
public String gE(Object o) {
if(o instanceof TextInputEditText) {
return ((TextInputEditText) o).getText().toString();
}
else if(o instanceof BootstrapEditText) {
return ((BootstrapEditText) o).getText().toString();
}
else if(o instanceof EditText) {
return ((EditText) o).getText().toString();
}
return null;
}
Make sure the base class is the last condition checked, otherwise the code to the extended class will become unreachable. For example - if you check TextInputEditText
condition after EditText
condition, it will always return true for EditText
condition and so will never check TextInputEditText
condition. This is because TextInputEditText extends EditText
.
UPDATE - use of instanceof
is considered bad practice. So, please use function overloading instead. Like -
public String gE(TextInputEditText text) {
return text.getText().toString();
}
public String gE(BootstrapEditText text) {
return text.getText().toString();
}
public String gE(EditText text) {
return text.getText().toString();
}