Search code examples
javaandroidperformanceclassfinal

Android Activity - Final class or not?


According to this SO question and performance benchmarks

We can assume that defining classes as public final class is better for performance.

Should we do the same with Android Activities, as they are mostly not used to subclassing ?

public final class LoginActivity extends Activity { ... }

Solution

  • It's usually a safe bet to make all your classes either abstract or final.

    This way, it's clear whether a class needs to be designed for inheritance.

    If you later find out that for some reason you need to inherit from a concrete (non-abstract) class - which is usually a sign that there's a bit of asymmetry in your overall design - you can still remove the final keyword and make sure that your class is fit for inheritance.

    So by doing this you eliminate the waste of having to design all your classes for inheritance, and at the same time you get maintainable code with clear intentions.