r/androiddev • • Nov 26 '16

Effective Java for Android (cheatsheet)

https://medium.com/rocknnull/effective-java-for-android-cheatsheet-bf4e3433889a
223 Upvotes

49 comments sorted by

View all comments

Show parent comments

7

u/Phreakhead Nov 26 '16

Please don't make classes final. I hate when I just want to extend some class to have an extra tiny bit of functionality, but I can't because the author either made it final or made some members private when they could have been protected. Android is the worst at this. So many useful widgets and gesture detectors and what not, all having to be copy/pasted if you want to improve them at all.

12

u/nhaarman Nov 26 '16

Instead, prefer composition over inheritance.

3

u/Zhuinden Nov 26 '16

There are times when you can't just wrap the code with some decorator to fix internal behavior.

So you have to go and copy the whole library into your project and make the tiny bits of changes that 'd be easily done if they were at least package-visible or protected, or the class weren't final.

Flow was the most annoying at this, I had to copy both Flow 0.12 back in the day, and also Flow 1.0-alpha into my project initially to fix its bugs, because most of its stuff just can't be fixed from the outside. Well, technically copying it and then putting it in a forked Github repository was the easiest solution, so there's that.

1

u/s73v3r Nov 28 '16

If the thing is fixing internal bugs, then that's how you should be doing it. Then send the fix to the maintainer. Don't subclass to fix behavior, as someone new to the project won't have any idea that you're using MyBaseClass instead of the one the library provides.

1

u/Zhuinden Nov 28 '16

Well I did file issues but I needed the fix sooner than 9+ months later