Start learning with our library of video tutorials taught by experts. Get started
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.
You can use interfaces in your own applications to model inheritance relationships. You create your own interfaces just like you do classes and in the interface definition, you add your method definitions, the method names and signatures, and you can also add final fields that is values that don't change at runtime. Interfaces can't have constructor methods or other elements of classes though. So interfaces should only be used in situations where you're modeling behavior and not dynamic management of data.
I'll give you a very brief example of this. I'll be working in a version of the application named creating interfaces. Let's say, for example, that I wanted to create an inheritance relationship between an interface called press and a specific class called OlivePress. I'll be creating an actual class named OlivePress2. Now I'll right click on press and choose New>Interface and I'll give it a name of Press and I'll click Finish. The first thing you'll see that syntactically different from a class is this keyword.
Instead of public class I'm saying public interface. Now within the class definition, I can define as many methods as I want. I'll go back to my existing OlivePress class and I am going to take some code from there. Let's say, for example, that I want all classes that implement my press interface to implement a getOil method. I am going to select and copy the method's signature, but not the implementation. Then I'll go back to press, I'll paste that in and I'll add semicolon at the end of declaration.
Notice that when I paste it in the code, Eclipse automatically added import statements for the collection and the Olive class. Those are required, but also notice that I'm not implementing the code here. I'm just saying what the name of the method is, what its arguments are, and what its return data type is. I'll go back to OlivePress and I'll take a couple more methods. I'll take to getTotalOil method, copy and paste it in, add the semicolon, I'll go back to OlivePress again, and I'll take setTotalOil, I'll copy and I'll paste.
Now when I save, I get an error. It tells me that methods defined in an interface can only be marked as public or something called abstract. Don't worry about abstract; I'll describe that later. What's important here is that when you declare method signatures in an interface, they must be public. You can't make them private because you can't implement the code here. So I'll change private to public and save my changes and the error goes away. Next I'll show you how to create a class that implements the Press interface.
I'll create a new class and I'll name this OlivePress2. Now you can actually implement multiple interfaces in Java. So you can add as many interfaces here as you want, but I am only going to implement one. I'll type in press and choose the Press interface that I just created, I'll click OK and Finish. When the class is generated by Eclipse, all of the methods that were defined in the interface are added automatically to the new class, just as in a superclass subclass relationship, you use the override annotation above all the method signatures that you are overriding.
It is an inheritance relationship and just like a superclass and a subclass, you can say that an OlivePress2 is a Press. To complete the coding process, I would add the implementing code in each of these methods. This would be enough to get you started though. Now, I will say that using interfaces in your own code is completely optional. If you like the idea of interfaces, use them; if you don't like them, ignore them. But it is important to understand how interfaces work because of the number of times you are going to encounter them in the core Java class library, such as with the collection an array list class that I described in a previous video.
If you want to create your own interfaces for your own applications though, this is the syntax and the process in Eclipse.
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.