Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
In previous videos, I've describe how to create an inheritance relationship between Java classes, superclasses that contain functionality, and subclasses that inherit that functionality. There is another way of creating inheritance relationship using an interface. An interface in Java lets you define the structure of a class including the names and signatures of the classes methods and any final fields that is class or instance variables that will always have the same values. You can then implement that interface with a class that you can actually instantiate.
The Java class library uses interfaces extensively. Here is an example of an interface you probably will use in your own applications, the collection interface. The collection interface is the high- level definition of objects that are a part of the Java collections framework and we've only used a number of classes in this framework including the array list. The collection interface defines the names and signatures of methods that are a part of all the classes in the list of implementing classes. I'll go down to the method list and you'll see that they look familiar.
These are the same methods that are a part of the array list class. The idea of an interface is that you're defining a contract. If you say that a class implements an interface, it must implement all of these classes and it must use the exact method names and the same number and data types of arguments and the same return data type. This then allows you to use the concept of polymorphism in your code. An array list can be seen either as an array list or as the interface that it implements, collection.
Let's see how you might use this in an actual application. I am in a project named UsingInterfaces that's a part of his chapter's exercise files. In this version of the application I have a call to a method named getOil that's defined in the class OlivePress. I am passing in an object as an array and this object is declared as an array list containing instances of the Olive class. In the OlivePress class, where I've defined that method, I'm using exactly the same data type array list. Now because array list implements the collection interface I can actually make this method a lot more flexible.
Instead of requiring an instance of the array list class, I can instead require an instance of the collection interface. So I'll change the data type in the method signature from array list to collection. After typing the name of the interface, I'll press Ctrl+Space and choose the collection interface and that causes the required import statement to be added to the top of the code. I'll then clean up my imports by going to Source>Organize Imports and that removes the import for the array list, which is no longer referred to in this class.
I'll save my changes and that rebuilds the project and you'll see that there are no programming problems as a result. I am up casting from array list to collection, from the more specific type to the more general type. And now this method can accept any instance of any class that implements the collection interface. In the Main class, I'm going to still keep the array list declaration here. I'm not going to change its collection and the reason is because the array list class has other methods that are not part of the contract that are not described in the collection interface.
One of the most important of these is the get method. This method is a specific part of array list and is not part of the contract and if I data typed this object as collection the compiler would not accept my call to get. But with that change I'll save and run the application and show you that it still works exactly as it did before. But now my getOil method is more flexible. I can pass in any instance of any class that implements the collection interface. Now you can create your own custom interfaces in your applications and your own implementing classes and I'll show you how to do that in another video.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.