Why do we have to use mplements?

Asked

Viewed 536 times

9

What’s the difference between using

btn.setOnClickListener(new OnClickListener() {

and

public class MainActivity extends Activity implements OnClickListener{

I’ve been doing an Android course and my teacher said that the Implements just helps to keep the code clean without making it necessary to use the OnClickListener in the middle of the code.
Is that all there is to it?

1 answer

11


The purpose of implements it is not to make the code cleaner, is just a reserved word that says that your class is implementing an interface, that is, your class is now the type that it implemented, with this comes what we call "Contract", which is a way of saying that the class that implements the interface is required to implement all the methods that the interface has stated.

Analyzing your case. In:

btn.setOnClickListener(new OnClickListener() {

You are creating an anonymous class that must follow the contract imposed by the Onclicklistener interface.

In :

public class MainActivity extends Activity implements OnClickListener{

You get your Mainactivity class to implement the Onclicklistener interface.

In both cases you will need to overwrite the method onClick(View v). Obviously, in the first case you will need to do this within your anonymous class and in the second case it will be within your Mainactivity class.

Whether this will make the code cleaner or not depends on it, it can do so if you have numerous buttons that may have something in common and it is therefore to their advantage to call the same method onView() within its Mainactivity class, thus:

btn.setOnClickListener(this);

However, it doesn’t make much sense that several buttons do exactly the same thing, which is why I said that it can be used when they have similar functions, but in doing so it will be necessary to check within the implemented method which was the button that called, so usually within this method you will have a switch or several ifs.

To the point that if your class did not implement it you would be required to "replicate" the code for all of your buttons. So:

btn.setOnClickListener(new OnClickListener() {
    //faça alguma coisa
});

If you only have one button, the situation is almost tied as you will only implement it once. For both cases the difference will be more your satisfaction with each of the ways to solve the problem.

More details of what an interface is and why they are useful can be found in this other answer: In object orientation, why interfaces are useful?

  • That’s what happens when I don’t use Mplements. At first I didn’t mind so much, but I thought it best to just do this "contract" as you said. It helped a little. Now I understand this question better.

  • @Rogerscorrêa added a link that explains a little better what an interface is, because if I explained it here I think it would be very comprehensive, which is unnecessary because it has already been answered in the linked topic. I hope you have understood the real difference between the two. To say that Mplements cleans the code is absurd! Its function is something far superior to that, perhaps clearing is a consequence, but it becomes something irrelevant close to its real purpose. Make sure the question has been fully resolved, anything ask for more details in particular.

  • Thank you very much Math. I will look at the link yes, knowledge is never more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.