5

I have an abstract base class, and two child classes; I have the "same" field in the two child classes, annotated each other with "different" annotations, and I want to put "up" the field into the base class, and add the annotations in the child classes.

Is possible? (following non-working pseudo-code)

abstract class Base {
    Object field;
}

class C1 extends Base {
    @Annotation1
    super.field;
}

class C2 extends Base {
    @Annotation2
    super.field;
}
Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
  • Is this question about the [AndroidAnnotations](https://github.com/excilys/androidannotations) library? (as tagged). – WonderCsabo Dec 17 '14 at 11:44
  • Yes. The question is generic, but my particular problem is correlated to the use of the android annotations library. – Vito De Tullio Dec 17 '14 at 12:50
  • Specifically: I have have 2 @EFragment-annotated class (with different layouts files... one for the portrait and one for the landscape versions of my fragment), each one extends a base class with some common logic. Most of my fragments are equivalent, also with some @ViewById()-annotated Views. Ideally I wanted to put the logic in the base class, where I declare my Views and access to them, and insert the annotation in the child classes – Vito De Tullio Dec 17 '14 at 12:56
  • You may want to add this information to the OP. – WonderCsabo Dec 17 '14 at 16:45

2 Answers2

2

You cannot "override" a field in java, so, no, strictly speaking, you cannot do what you want.

In general, it seems strange that "same" field needs different annotations, suggesting that there is, probably, something wrong with your design, but hard to tell without knowing specifics.

Most annotations work with accessor methods same way they do with member fields. So, what you can do is make your field private, and provide setField() and getField() accessors for it. Then you can override those in subclasses, and annotate differently.

Dima
  • 39,570
  • 6
  • 44
  • 70
0

Let's say you have these layouts:

fragment1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/commonView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/viewInFragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

fragment2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/commonView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/viewInFragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Then you can have these Fragment classes:

@EFragment
public class BaseFragment extends Fragment {

    @ViewById
    TextView commonView;

    @AfterViews
    void setupViews() {
        // do sg with commonView
    }
}

@EFragment(R.layout.fragment1)
public class Fragment1 extends BaseFragment {

    @ViewById
    TextView viewInFragment1;

    @Override
    void setupViews() {
        super.setupViews(); // common view is set up

        // do sg with viewInFragment1
    }
}

@EFragment(R.layout.fragment1)
public class Fragment2 extends BaseFragment {

    @ViewById
    TextView viewInFragment2;

    @Override
    void setupViews() {
        super.setupViews(); // common view is set up

        // do sg with viewInFragment2
    }
}
WonderCsabo
  • 11,947
  • 13
  • 63
  • 105