How To Implement A Java Interface (Explained Step By Step)
► Free Selenium PDF Book: https://www.rexjones2.com/book-seleni...
► Free Java PDF Book: https://www.rexjones2.com/book-part-1...
► All Paperback & eBooks: http://tinyurl.com/Rex-Allen-Jones-Books
Social Media Contact
✔ YouTube / rexjonesii
✔ Twitter / rexjonesii
✔ LinkedIn / rexjones34
✔ GitHub https://github.com/RexJonesII/Free-Vi...
✔ Facebook / jonesrexii
► Download Presentation, Code, and Transcript via Website = https://www.rexjones2.com/java-interf...
► Download Presentation, Code, and Transcript via GitHub = https://github.com/RexJonesII/Free-Vi...
Transcript
Let’s look at 2 email service providers: Outlook and GMail. With Outlook, we see a lot of functions such as File, Home, Send/Receive, Folder, View, and Help. If we wanted to create a new email, we can click New Email. If we wanted to close, we can click X in the top right corner or go to File then click Exit.
Those same functions are available with GMail and other email service providers. In GMail, to create a new email, we select Compose. It’s the same function in Outlook but just a different name. The Spam folder is used to store email that we don’t want in our inbox. Outlook has this same function but it’s called Junk. Although both providers have functions that operate the same, the name is different and implementation details are different.
For consistency, we can create an interface so Outlook and GMail have the same name with their own unique implementation. Outlook has 3 methods: newEmailMessage, openJunkFolder, and closeOutlook.
GMail also has 3 methods but different names: composeEmailMessage, openSpamFolder, and closeGmail.
Recall from the introduction, an interface is a contract with a class. It’s an agreement that determines what a class must do but not how the class must do it. Therefore, both classes will have the same method name but different details.
To create an interface, we go to New - Interface. The name will be EmailServiceProvider then click Finish. It looks like a class but we see public interface and not public class. We create a method by writing public void then the method name createEmailMessage () add a ; semicolon.
Writing keyword abstract after public is optional. As a convention, we do not write abstract. It’s an assumption, that all methods in an interface are abstract although we can write default methods and static methods. For example, if I remove the semicolon then add a body which start and stop with the curly braces. An error states “Abstract methods do not specify a body” but show 3 quick fixes which include changing the method to default or static. Remove the braces then add back the semicolon. Abstract methods define the signature of a method but no implementation for the method. Some more methods that we can add to this interface are public void openJunkSpamFolder (); and public void closeEmailProvider ();.
So, this interface says; all implementation classes should be able to create an email message, open a junk spam folder, and close the email provider.
Now, let’s implement the interface. Go to Outlook and write implements EmailServiceProvider. Instantly, we see an error “The type Outlook must implement the inherited abstract method EmailServiceProvider.openJunkSpamFolder()”. All 3 methods are recognized as an abstract method. The 2 quick fixes are Add unimplemented methods and Make type ‘Outlook’ abstract. Let’s add the unimplemented methods. We see each method from the interface EmailServiceProvider with an Override annotation.
This is where the Outlook class can implement its unique details for each method. I’m going to copy and paste the print statements. Outlook – New Email, Outlook – Open Junk Folder, and Outlook – Close Email.
The same for Gmail, it implements EmailServiceProvider and let’s Make the type abstract. The keyword is added before class. We should not make the class abstract because it can lead to an error when creating an object. Objects cannot instantiate an abstract type. Remove abstract, save, then select Add unimplemented methods. Also copy and paste the print statements. GMail – Compose Email, GMail – Open Spam Folder, and GMail – Close Email Provider.
The contract agreement between the interface and each class has been fulfilled. Look what happens when the interface breaks the contract agreement by adding another abstract method.
public void saveDraftFolder(); then click Save. We see an error in the Outlook class and GMail class. The error exists because both classes was not part of the original agreement with saveDraftFolder.
#Java #InterfaceInJava #InterfacesExplained #Object-OrientedProgramming
Смотрите видео ✔ How To Implement A Java Interface (Explained Step By Step) | (Video 120) онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Rex Jones II 29 Июль 2020. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 367 раз и оно понравилось 9 посетителям.