![]() So I've created another interface named ExtendedContract. Yep, an interface can extend another interface. Okay, so we got to cover one final point that's really important. And you'll notice term1 and term2 are printed to the console. We'll execute this code after we save everything. And then inside the body of the method, we can use that interface to invoke term1 and term2 on our object. ![]() So we can pass our contract object into it. So you'll notice the method signature for the print terms method has a parameter of type contract. And then we can take that object and pass it to anywhere the contract interface is accepted. This works because the implementation implements the contract interface. And then, I'm going to instantiate the implementation and assign it to the contract. So to do that, I'm going to declare a variable of type contract. All right, with that in place, we're going to now navigate over to the application class and here, we're going to use our new contract interface and its implementation. And then once again, I'm just going to add some console output to help us understand the execution of our code. Let's go ahead and satisfy the contract by adding the method. So we added a new role or term to the contract, and it's not satisfied. ![]() It's because there is a new method on our contract interface that isn't found on the implementation. So I'm just going to add another abstract method named term2, and look what happened. Let's see what happens if we add a new abstract method. So now the implementation has satisfied the terms of the contract interface. That way, when we execute this code, we can see what methods invoked. Now, inside of this method, I'm just going to print some console output. Just click on add unimplemented methods, and the IDE will automatically add it to the body of your class. So let's go ahead and add this method to the class. When the compiler sees the class doesn't match the template, it complains. It's kind of like we're saying the interface, or the contract, is a template for what methods should be found on the implementation class. This method is required because the implementation class implements the contract interface. So if we hover over the error, you'll notice that it's complaining because our implementation hasn't defined a method named term1. Now, as soon as we do that, you'll notice the compiler in my IDE start to complain. If we would like this class to implement the contract interface, all we need to do is type implements, and then the name of the interface, which in this case, is Contract. So on the right-hand side of my screen, you'll notice I have a class named Implementation. Any abstract method on the interface must appear on a class known as an implementation that implements the interface. Abstract methods define the roles or terms for a class that wants to implement the interface. So let's go ahead and add one to the interface named term1. On the interface we can define abstract methods. And for our purposes right now, we're going to say that it cannot define methods with a body that contain code. An interface is an abstract type, so that means we can't create an instance of it. I chose that name because the idea of a contract is commonly used to explain an interface. On the left-hand side of my screen, I have an interface named Contract. ![]() That way, you're successful with collections. So let's just make sure you're solid on the concept. When working with collections, it's important to have a good understanding of interfaces because they're used heavily by the framework. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |