As I stated in another post (Fragment to Activity Communications) I don't understand interfaces. I am trying to understand OOP and am reading through this site's posts and books such as "Thinking in Java" by Bruce Eckel (TiJ). Perhaps a big part of my problem is that I haven't been trained in OOP. I'm an electrical engineer who has written assembly language operating systems for 8 bit controller embedded systems. Procedural languages are relatively easy to understand, OOP still has some mysteries, Interfaces are one of them. I have two questions in this post.
1. Interface as template:
In TiJ, as in most explanations of interface, Bruce writes:
"An interface says, "All classes that implement this particular interface will look like this." Thus, any code that uses a particular interface knows what methods might be called for that interface, and that’s all. So the interface is used to establish a "protocol" between classes. (Some object-oriented programming languages have a keyword called protocol to do the same thing.)"
From What is an interface in Java?, I see the following, which seems to be saying the same thing as immediately above:
"What it's useful for: One of its uses is for it to be used as a face for a service. When two parties work together to form a service-requester & service-provider kind of relationship, the service provider provides the face of the service (as to what the service looks like) in the form of an interface."
To me, it would be analogous to providing a person who wants to build a computer (analogous to the program) a set of computer parts, even parts that they wouldn't use (the hardware analogy to an SDK). In that HW set, one would find a bunch of FPGAs (analogous to interfaces) that need to be programmed as the CPU. FPGAs are flexible, they can be almost any CPU, in SW, and be reprogrammed in-circuit. So, Interfaces seem to provide a "template" that the programmer can code as required for their specific circumstance.
Is this close?
2. Interface to span hierarchies
In Android, it seems intended that one use interfaces to provide communication between Fragments via their Activities (https://developer.android.com/training/basics/fragments/communicating.html). To me, that seems to have something to do with (TiJ):
"Whenever a method works with a class instead of an interface, you are limited to using that class or its subclasses. If you would like to apply the method to a class that isn’t in that hierarchy, you’re out of luck. An interface relaxes this constraint considerably. As a result, it allows you to write more reusable code."
From Proper interface use and explanation I see the following, which seems to be saying the same thing as immediately above:
"A nice example for use of inheritance and interfaces will be: Lets say you are building a software for electronics devices. And the scenario is like (parent class -> inherited class) : Gadgets -> Electronic Gadgets -> Telephony Gadgets -> Mobiles -> Smart Phones -> Tablest. And say Mobiles, Smart Phones and tablets have a common feature of FM-RADIO. This feature is available in other Gadgets which are not Telephony Gadgets. Now it will be perfect to use FM-Radio as an interface. Every gadget will provide their own definition of FM-Radio but all will share same functionality."
So, in regards to communicating between Fragments, the hierarchy of Activity is:
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
While the hierarchy of Fragment is:
java.lang.Object
↳ android.app.Fragment
Inside the compiler, do interfaces provide the utilty to connect between those different hierarchies to provide communication between Fragments?