1

I have a class that extends TextView, it basically adds some feathers to display text with different colors ,animation etc...

Now I'm working with other custom view that don't uses the Android TextView but uses a Label view that not related to TextView at all .

What I want to do is to copy all the logic and methods to a new class that will extend Label and just to change some few things: colors are not int type but Color type and so on. the setText(text) works the same.

Do I have to really copy-paste all the class and change some stuff or I can somehow use one logic and make some few changes on the new class. When it will extends Label the setText(text) will act the same on Label (class Label have that method too)

public class TextDisplayTextView extends TextView {

public TextDisplayTextView(Context context) {
        super(context);
        init();
    }

    public TextDisplayTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

private void setText (String text) {

// do some effects
setText(txt);
}

private void setTextColor (int color) {

// do some color effects
setTextColor(color);
}

.
.
.

}

The new class - TextDisplayLabel

public class TextDisplayLabelView extends Label {

public TextDisplayLabelView(Label.Design design ) {
        super(design);
        init();
    }

private void setText (String text) {

// do some effects
setText(txt);
}

private void setTextColor (Color color) {

// do some color effects
setLabelColor(color);
}

.
.
.

}

EDITED: I want to use it in two cases

In class that uses TextView :

TextDisplayTextView textV = (TextDisplayTextView) mLayout.findViewById(R.id.textDisplay);
    mText.setText()

In class the uses Label :

Label.Design designStyle = new Label.Design designStyle();
TextDisplayLabelView textL = new TextDisplayTextView (designStyle);
textL.setText();
RonTr
  • 137
  • 10
  • Maybe using an abstract class and inversion of control ? Could you be a little bit more specific regarding what you are looking for ? How does each class is used by the client code ? – Francis Toth Aug 26 '15 at 21:21
  • I'm creating instances with those classes and uses methods that implemented there like `setColorWithA()`, `showTextAnimation()`... like that: `mTextView.setColorWithA()` or `mLabelCustomView.setColorWithA()` – RonTr Aug 26 '15 at 21:45
  • So basically, you have a method setColorWithA() which sets a specific color depending on if its a textView or a labelCustomView ? Please provide some code where you're doing that. What would be ideal for your usecase ? The abstraction will depend on how you are using theses classes. As noticed by Sanjay Manohar, multiple inheritance does not exist in Java, however there are some strategy to deal with this kind of problem, depending on what you need. – Francis Toth Aug 27 '15 at 01:51
  • @FrancisToth I added code to show hoe should I use it, basically in objects that uses `TextView` use first option and objects with `Label` uses second option – RonTr Aug 27 '15 at 20:10

1 Answers1

0

Java does not have multiple inheritance. This is a contentious point. That would be a standard way to implement what you're suggesting.

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

But there are patterns to get round it.

One way is to use a helper class. That class will contain the actual implementations of the "jointly used code".

Another way is to use static methods for the generic aspects.

You will, however, have to duplicate calls to that class, in the individual interface implementations.


Edit

Given your added info, I here are some possibilities

public class TextDisplayLabelView extends Label {
  public void setText(String text){
    MyTextHelper.setTextHandler(this,text);
    super.setText(text);
  }
}
public class MyTextHelper {
   public static void setTextHandler(View item, String text){
     // do stuff that works on any type of item
   }
}

OR

public class TextDisplayLabelView extends Label implements IMyTextHelper {
  public void setText(String text){
    MyTextHelper.setTextHandler(this,text);
    super.setText(text);
  }
}
public interface IMyTextHandler{
  static class MyTextHelper{
    public static voidTextLabel setTextHandler( ... ){ ... }
  }
}

or in Java 8 you can use static methods directly in interfaces.

Aside:

Note that these really are substitutes for multiple inheritance. For example in python you would inherit like this:

class TextHelper extends Object:   methods to work on text
class TextDisplayLabelView extends Label,TextHelper
class TextDisplayTextView  extends TextView, TextHelper

where the latter two classes would inherit the methods to work on text, as well as being fully fledged View components.

A different possibility is to use a proxy object. But this doesn't actually override the methods of the actual text object, so may not do what you need.

class GenericTextObject implements {
  public View proxy;
  public void setText(String text){
    if (proxy instanceof TextView){
       ((TextView)proxy).setText(text)
    }else if(proxy instanceof Label){
       ((Label)proxy).setText(text);
    }

    doExtraThings();
  }
}
Community
  • 1
  • 1
Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
  • Even if there was an option to use multiple inheritance I don't want to have a class that have methods from `TextView` and from `Label`. Can you show some code how to implement it with static methods,interface? – RonTr Aug 27 '15 at 20:17