Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Using reflection and polymorphism with ViewHolder

I have a json with class name, something like this:

{
   "view_class" : "com.view.MyClass"
}

And I have this hierarchy:

    public class Config {
         public String viewClass;
         ...
    }

    public interface ViewHolderInterface {
         void bindValue(...);
    }

    public class SuperMyClass extends RecyclerView.ViewHolder 
implements ViewHolderInterface {
       ... 
    }

    public class MyClass extends SuperMyClass {
       ... 
    }

I read a json file and I generate a "Config" object, so the idea is this:

public void bindViewHolder(SuperMyClass holder, Config config) {

    Class viewHolderClass;

    try {
        //row.viewHolderClass = "com.view.MyClass"
        viewHolderClass = Class.forName(config.viewClass);

    } catch (final Exception exception) {

    }
    // Here I need to "cast" a holder to MyClass and execute the method bindValue.
    final ViewHolderInterface viewHolderInstance =
        (ViewHolderInterface) viewHolderClass.cast(holder);
    viewHolderInstance.bindObjectValue(...);
}

I have tried to do this cast using reflection but I this throws an exception:

java.lang.ClassCastException: SuperMyClass cannot be cast to MyClass

I think that my error is using reflection and the same time try to up-cast, but I have not found another way to do this.

Consider that there may be more than one "view_class" type (remember that this class is an implementation of a ViewHolder).

Any idea?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think that my error is using reflection and the same time try to up-cast, but I have not found another way to do this.

Your reflection is ok, but the downcasting is not. Take a look at this question: explicit casting from super class to subclass

Consider that there may be more than one "view_class" type (remember that this class is an implementation of a ViewHolder).

You can inspect the object instance in runtime to find out its class. You need not tell the class by outside means.

You can try a safer approach like:

// mind the comparison order from subclass upward
if (MyClass.class.isInstance(holder)) {
    MyClass myClassHolder = (MyClass) holder;
} else if (SuperMyClass.class.isInstance(holder)) {
    SuperMyClass superMyClassHolder = (SuperMyClass) holder;
} else if (ViewHolder.class.isInstance(holder)) {
    ViewHolder viewHolder = (ViewHolder) holder;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!