Implement Multiple Interfaces To Overcome Diamond Inheritance Problem
► 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 Code, Presentation, & Transcript via Website = https://www.rexjones2.com/multiple-in...
► Download Code, Presentation, & Transcript via GitHub = https://github.com/RexJonesII/Free-Vi...
Transcript
Multiple implementation of interfaces is a way for a class to implement more than 1 interface. That’s important because inheritance cannot extend more than 1 class but an interface can extend more than 1 interface. In this diagram, we see 1 Super Class and 3 Sub Classes. Each Sub Class inherits the Super Class. Let’s look at this diagram using a Store example.
Store is the parent class while Food Store, Clothes Store, and Medicine Store are the child classes. With inheritance, there is no way to combine all stores into 1 Combo Store. It is impossible because we are not allowed to extend more than 1 class. This is called a diamond problem.
Do you see the diamond? Combo Store will not inherit Food, Clothes, or Medicine although it’s possible in real life. However, we can combine all stores when 1 class which is Combo Store implements more than 1 store. There is no limit to the number of interfaces a class can implement.
In Eclipse, we see the FoodStore has 2 abstract methods: sellFruits and sellVegetables.
The Medicine Store also has 2 abstract methods and 1 default method: sellPresecription and sellVitamins are the abstract methods, ‘payCashier’ is the default method with details to pay a Pharmacy Technician.
Clothes Store is similar to Medicine Store. It has 2 abstract methods: sellShoes and sellShirts and the same default method named ‘payCashier’ but different implementation details.
To combine all 3 interface stores, we go to the Combo Store then it implements FoodStore, ClothesStore, and MedicineStore. Just like when implementing 1 interface, we must add all unimplemented methods. There should be 6 overridden methods: sellPrescription, sellVitamins, sellShoes, sellShirts, sellFruits, and sellVegetables.
Save and there’s 1 more error “Duplicate default methods named payCashier with the parameters () and () are inherited from the types MedicineStore and ClothesStore”.
For a quick fix, we can override the default method or change the parameters.
This is also a diamond problem because both interfaces have the same default method name payCashier and the compiler cannot decide which super method to use. Override payCashier from ClothesStore. Go to the method and we see Override – ClothesStore.super.payCashier().
Recall that a class cannot extend more than 1 class but an interface can extend more than 1 interface. Let’s go to the FoodStore interface. It extends another interface called EmailServiceProvider which is another interface from the previous session How To Implement An Interface.
Save and ComboStore will have a different error because the abstract methods from EmailServiceProvider must be implemented. Add unimplemented methods then go to the bottom and we see each method: createEmailMessage, openJunkSpamFolder, closeEmailProvider. In this example, multiple interfaces are extended after adding a comma and another interface name. The interface is Day then it extends 2 interfaces: Week and Month.
That completes the Java Object-Oriented Programming Series. We covered 4 traits of the OOP’s concept: Encapsulation, Inheritance, Polymorphism, and Abstraction. I hope you learned something from this OOP series. If you did, Subscribe, Connect, and Follow me because I have so much more content to share with our Testing Community.
#Interface #Java #ObjectOrientedProgramming #SeleniumWebDriver
Информация по комментариям в разработке