7 days of WordPress plugins, themes & templates - for free! Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.5 Default and Static Interface Methods

There are a few important changes to interfaces in Java 8. This lesson shows you how to make the most of them.

2.5 Default and Static Interface Methods

Hello, and welcome back. In this lesson, you are going to learn about two new features that were added to interfaces in Java 8. Before we dive into those features, let's first create a normal interface. So I'm going to right click here and select New > Java Class. For the Kind option, make sure you select Interface and give the interface a name. And then press OK. Let's add two methods to this interface. I'll call the first method getAuthor, and the second method getAuthorId. Now let's say that we want all the activities in our app to implement this interface. So I'll go to MainActivity and use the implements keyword. Of course, we must now define both the methods we mentioned in the interface. So select both of them and press OK. For the sake of completeness, I'm going to type in my name here and say that my ID is 1. Similarly, we can go to the StreamsActivity class and again implement my interface. So there's nothing new about any of this. If you have been using Java 7, this is something you probably do everyday. Now let's say I want to add an extra method to my interface. I wanna call it getProjectOwner. As soon as we make this change, both our activities will have a compilation error. We must implement the new method in both of them to fix the error. This is quite tedious, isn't it? Imagine if we had 10 or 20 activities, implementing the interface will take a lot of time. You would have to go and manually add the method to all of them individually. Until you do that, your app will not compile. In Java 8, there's a fix to this problem. What we can do is add the default keyword to this method, and provide a default body right inside the interface. I'll just return Tuts+ here. And now if you go and check the activities, there will be no errors anymore. So this is a very handy feature. You can, if you want to, overwrite the method and provide custom implementation, but that's totally optional. So this is called a default method. The main use case for default methods is that you can now add new functionality to your interfaces without worrying about breaking older code. Okay, the next feature we are gonna look at is support for static methods and interfaces. In Java 8, you can add a static method inside an interface. The syntax is identical to the syntax you would have to use while creating a static method in a class. So there's nothing new about it. You just have to use the static keyword. But note that you must always include a body for your static methods in your interfaces. They cannot be abstract. Back in our activities, which are implementing the interface, there's really no change. But if I want to, I can call the static method here. The important thing to note about static methods is that they belong to the interface body. You cannot directly call getProjectId from this class, even though it implements the interface. Similarly, you can't overwrite an interface's static method inside any class. So if I try to choose Generate > Implement Methods here, I'll just not be able to find the getProjectId method. Static methods are a good way to add functionality to your interfaces if you want to be sure that no one can overwrite it. You probably won't be using this feature too often, but it's good to know that it exists. You now know about default methods and static methods in interfaces. If you are someone who develops libraries for Android, I'm sure you're gonna love these methods.

Back to the top