0

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?

Woton Sampaio
  • 469
  • 6
  • 24
  • 1
    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 '18 at 17:14
  • @Turing85 I did not quite understand how to do, can you show me an example? – Woton Sampaio Jun 30 '18 at 17:15
  • Just look for a tutorial on inheritance in Java, e.g. [this one from Oracle](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html). – Turing85 Jun 30 '18 at 17:17
  • @Turing85 Cool, but what if they were buttons? How could I do such a method? – Woton Sampaio Jun 30 '18 at 17:17
  • @Turing85 and your fist example, how i convert views for type Text for call the method? – Woton Sampaio Jun 30 '18 at 17:19
  • I do not understand your question wrt. buttons. Buttons are just classes aswell, no special rules. To your other question: this smells like a broken desing. For a more depth analysis, you would need to show your code and what you do with the parameters. – Turing85 Jun 30 '18 at 17:23

4 Answers4

2

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();
}
Sanchita Santra
  • 359
  • 1
  • 2
  • 9
  • 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 '18 at 18:13
  • 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 '18 at 18:16
  • 1
    Also see e.g. https://stackoverflow.com/questions/30894002/is-it-good-practice-to-often-use-instanceof for a discussion of why using `instanceof` like this is often considered a bad practice. – Radiodef Jun 30 '18 at 18:20
  • So, the accepted answer should be function-overloading. Should I edit my answer? – Sanchita Santra Jun 30 '18 at 18:28
2

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.

JSharma
  • 113
  • 8
  • This is a great solution too. +1 – Woton Sampaio Jun 30 '18 at 17:54
  • well come Woton Sampaio :) – JSharma Jun 30 '18 at 17:58
  • 1
    This solution should be preferred over the one with `instanceof` that leads to a unclear API. – Robert Hume Jun 30 '18 at 18:07
  • @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 '18 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 '18 at 18:18
0

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

Minh Trần
  • 356
  • 4
  • 13
0

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.

Radiodef
  • 37,180
  • 14
  • 90
  • 125