3

What is the benefit of making RecyclerView.OnScrollListener an abstract class? Now you actually have to create a separate class instead of simply implementing the listener's method in the Fragment/Activity...

This question was asked before here but the answer just quotes Google which doesn't seem to really answer what benefits there actually are. Google stated:

Abstract classes allow frameworks to add new methods without breaking existing implementations.

But is that necessarily true? Adding more methods to an abstract class would also break existing implementations because if not implemented then the class extending it would have to be abstract as well... so it's the same problem as with adding methods to interfaces.

The diff for when they changed it here also says

I also changed it to be an abstract class instead of an interface to make future changes easier.

but again I don't see how that makes any sense. Does anyone understand the benefit of this change offered in this context? And if it really was better, why are all of the other listeners implemented as interfaces rather than abstract classes?

edit: note there aren't any concrete implementations in the class either, so I see no inherent benefit of using abstract class here vs an interface

Community
  • 1
  • 1
u3l
  • 3,284
  • 4
  • 29
  • 49
  • I agree, that an interface should have been provided. As far as future proofing is concerned, they may be planning to 1. add some utility methods 2. Have the parent class track some local scroll information. Its hard to guess right now. RecyclerView is still undergoing changes, so it would be a while before its API is finalized. – S.D. Jun 03 '15 at 15:16

3 Answers3

1

There are a couple advantages of making a blank abstract class with no implementation in the methods.

The first reason to make it abstract is you can't create the object itself. You always have to have a class that extends it which means you can't really accidentally create one. (Although you could always just go new RecyclerView.OnScrollListener(){} so maybe that's not an advantage in this case).

If there are a lot methods, but you only need to implement one, you only have to override the one method instead of overriding all of them and leaving them blank. Usually this is done by having an interface and then having an abstract class that implements the interface so you can pick and choose which version is right for you. They apparently opted to not do it this time.

A final advantage, and one that they're talking about in your question, is that if the developer adds methods, they won't break existing code. RecyclerView is currently being developed still, and it's part of the support library meaning it's not part of official Android releases. OnScrollListener() currently has two methods, onScrollChanged() and onScrolled(). If it was an interface, you would be forced to override both those methods in order to compile. Now, six months down the road, they decide to add more functionality like onScrolling(). Suddenly your code won't compile when you update the support library. You have to go through each file that uses an onScrollListener() and add this new method that you weren't even using in the first place. Keeping the class abstract means that they can add methods as they please, and no existing code will stop compiling.

The only real disadvantage is you can't add it to an Activity or Fragment like you could an OnClickListener, so you have to make it an inner or anonymous class.

DeeV
  • 34,764
  • 7
  • 103
  • 92
0

I think you're a bit confused as to what abstract classes are.

By having RecyclerView.OnScrollListener as an abstract class, it allows you to have a listener for a scrollbar without you having to write all those methods yourself. This is because an abstract class allows default implementations for the functionality.

If you wanted to customize the default implementations, then all you have to do is overload the method you want to customize. You don't have to create any extra classes. All you have to do is extend the OnScrollListener class into your activity and bam, now you have the listener.

Lawrence Aiello
  • 4,204
  • 5
  • 17
  • 31
  • Right yeah, what I thought they meant by extending functionality later on was adding more `ABSTRACT` methods, not concrete ones. Does it really make sense to give pre-written functionality? What would the concrete methods of the class do by default -- I mean the whole point of listeners is to define custom behaviour triggered on certain events right? – u3l Jun 03 '15 at 14:56
  • And if you look at the link, `RecyclerView.OnScrollListener` has absolutely no concrete methods. Sure they could add some in later... but what would they do exactly is my point? – u3l Jun 03 '15 at 14:57
  • You're asking me questions that only someone working on these APIs would know. – Lawrence Aiello Jun 03 '15 at 15:00
0

But is that necessarily true? Adding more methods to an abstract class would also break existing implementations because if not implemented then the class extending it would have to be abstract as well... so it's the same problem as with adding methods to interfaces.

Actually it depends whether the new method are declared as abstract or not. If you check the RecyclerView.OnScrollListener implementation you will see that just the class is abstract. As long as they don't add abstract methods is safe.

rciovati
  • 26,385
  • 6
  • 77
  • 99