Search code examples
androidabstract-classsubclasssuperclass

What to replace "this" in subclass methods with, when extracting them to an abstract superclass?


I have two Android activities A and B, which have a lot of methods in common. These methods use this in their method implementations. I am now abstracting these activities into an abstract superclass which extends Activity.

Should I leave the this as it is in the superclass's methods or do I need to change it to something else to ensure the right context is passed to the appropriate methods? If I need to change this to something else, please tell me what I should replace it with?


Solution

  • Should I leave the this as it is in the superclass's methods or do I need to change it to something else to ensure the right context is passed to the appropriate methods?

    this refers to the current instance/object. Since abstract classes can't be instantiated, the only remaining possibility is the concrete implementation. So you don't need to replace this