Dynamic Parameter with “or”


Dynamic Parameter with “or”



I'd like to know if it's possible to do something like this:


public String gE(EditText text || TextInputEditText text || BootstrapEditText text){
return text.getText().toString();
}



Perceive the use of "or", the parameters basically have the same attributes, but I would like a way that I can pass one or the other without having to create one for each element. Is it possible to do this in java?





Pass a common superclass reference instead of the concrete implementations: public String gE(Text text) (assuming the existance of a common superclass Text).
– Turing85
Jun 30 at 17:14



public String gE(Text text)


Text





@Turing85 I did not quite understand how to do, can you show me an example?
– Woton Sampaio
Jun 30 at 17:15





Just look for a tutorial on inheritance in Java, e.g. this one from Oracle.
– Turing85
Jun 30 at 17:17






@Turing85 Cool, but what if they were buttons? How could I do such a method?
– Woton Sampaio
Jun 30 at 17:17





@Turing85 and your fist example, how i convert views for type Text for call the method?
– Woton Sampaio
Jun 30 at 17:19





4 Answers
4



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.


TextInputEditText


EditText


EditText


TextInputEditText


TextInputEditText extends EditText



UPDATE - use of instanceof is considered bad practice. So, please use function overloading instead. Like -


instanceof


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();
}





Your comments at the end don't make any sense. If TextInputEditText extends EditText, then there's no reason to have a TextInputEditText block at all, because it's handled by the EditText block.
– Radiodef
Jun 30 at 18:13


TextInputEditText extends EditText


TextInputEditText


EditText





Yes Radiodef, I realized that. But then I left it in case, the developer needs to keep separate codes for different types of objects. Let's say getEditableText() for TextInputEditText and getText() for EditText.
– Sanchita Santra
Jun 30 at 18:16


getEditableText()


TextInputEditText


getText()





Also see e.g. stackoverflow.com/questions/30894002/… for a discussion of why using instanceof like this is often considered a bad practice.
– Radiodef
Jun 30 at 18:20


instanceof





So, the accepted answer should be function-overloading. Should I edit my answer?
– Sanchita Santra
Jun 30 at 18:28



You can't use ||(or) operator as function argument in java but you can achieve this by using concept called function over loading.



Like below:


public String gE(EditText text){
return text.getText().toString();
}
public String gE(TextInputEditText text){
return text.getText().toString();
}
public String gE(BootstrapEditText text){
return text.getText().toString();
}



In function overloading you can same name function with different argument or parameter name.





This is a great solution too. +1
– Woton Sampaio
Jun 30 at 17:54





well come Woton Sampaio :)
– JSharma
Jun 30 at 17:58





This solution should be preferred over the one with instanceof that leads to a unclear API.
– Robert Hume
Jun 30 at 18:07


instanceof





@RobertHume The problem is that I would have to write one thing 3 times, you know why? Imagine that instead of directly returning the text I want to do a treatment, this way I would have to write for the 3 methods, the other answer was to just create a string that receives the value of one of those fields, make the treatment in it and return it , without having to write 3 times, got it?
– Woton Sampaio
Jun 30 at 18:12





@Woton Sampaio you can write these three methods so that their first line retrieve the String value, then you can write a fourth method to do anything you want with that string, and call this single method from the other three.
– Robert Hume
Jun 30 at 18:18



Absolutely you can use an interface or a parent class of all of them which contains getText method





How i can do it?
– Woton Sampaio
Jun 30 at 17:29



If you have a class hierarchy that's like this:


public class EditText {
public CharSequence getText() {...}
}
public class TextInputEditText extends EditText { }
public class BootstrapEditText extends EditText { }



Then you can use this:


public String gE(EditText a) { return a.getText().toString(); }



And if you have a class hierarchy that's like this:


public class TextView {
public CharSequence getText() {...}
}
public class EditText extends TextView { }
public class TextInputEditText extends TextView { }
public class BootstrapEditText extends TextView { }



Then you can use this:


public String gE(TextView a) { return a.getText().toString(); }



Subclass types can be passed as arguments to methods which take their superclass type.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API