| Start |
|---|
| Implementing multiple interfaces A Java class can implement multiple interfaces. Notice how the Note:
|
| Declaring and Implementing an Interface Declaration of an interface using the Override Annotation This forces the compiler to check that we are overriding and prevents the program from defining a new method or messing up the method signature. Interfaces are implemented using the In the example, classes Using the interfaces |
| Extending an interface An interface can extend another interface via the Now a class implementing Extending multiple interfaces Unlike classes, the In this case a class implementing |
| Usefulness of interfaces Interfaces can be extremely helpful in many cases. For example, say you had a list of animals and you wanted to loop through the list, each printing the sound they make. One way to do this would be to use interfaces. This would allow for the same method to be called on all of the classes Any class that We now have three different classes, each of which has a Because each of these is an Because the order of the array is Interfaces can also be used as the return value for functions. For example, returning a Interfaces are also useful for extensibility, because if you want to add a new type of |
| Default methods Introduced in Java 8, default methods are a way of specifying an implementation inside an interface. This could be used to avoid the typical "Base" or "Abstract" class by providing a partial implementation of an interface, and restricting the subclasses hierarchy. Observer pattern implementationFor example, it's possible to implement the Observer-Listener pattern directly into the interface, providing more flexibility to the implementing classes. Now, any class can be made "Observable" just by implementing the Observable interface, while being free to be part of a different class hierarchy. Diamond problemThe compiler in Java 8 is aware of the diamond problem (opens new window) which is caused when a class is implementing interfaces containing a method with the same signature. In order to solve it, an implementing class must override the shared method and provide its own implementation. There's still the issue of having methods with the same name and parameters with different return types, which will not compile. Use default methods to resolve compatibility issuesThe default method implementations come in very handy if a method is added to an interface in an existing system where the interfaces is used by several classes. To avoid breaking up the entire system, you can provide a default method implementation when you add a method to an interface. This way, the system will still compile and the actual implementations can be done step by step. For more information, see the Default Methods (opens new window) topic. |
| Modifiers in Interfaces The Oracle Java Style Guide states: Modifiers should not be written out when they are implicit. (See Modifiers (opens new window) in Oracle Official Code Standard (opens new window) for the context and a link to the actual Oracle document.) This style guidance applies particularly to interfaces. Let's consider the following code snippet: VariablesAll interface variables are implicitly constants with implicit Methods
After all of the above changes have been applied, we will get the following: |
| Using Interfaces with Generics Let's say you want to define an interface that allows publishing / consuming data to and from different types of channels (e.g. AMQP, JMS, etc), but you want to be able to switch out the implementation details ... Let's define a basic IO interface that can be re-used across multiple implementations: Now I can instantiate that interface, but since we don't have default implementations for those methods, it'll need an implementation when we instantiate it: We can also do something more useful with that interface, let's say we want to use it to wrap some basic RabbitMQ functions: Let's say I want to use this IO interface now as a way to count visits to my website since my last system restart and then be able to display the total number of visits - you can do something like this: Now let's use the VisitCounter: When implementing multiple interfaces, you can't implement the same interface twice. That also applies to generic interfaces. Thus, the following code is invalid, and will result in a compile error: |
| Strengthen bounded type parameters Bounded type parameters (opens new window) allow you to set restrictions on generic type arguments: But a type parameter can only bind to a single class type. An interface type can be bound to a type that already had a binding. This is achieved using the This strengthens the bind, potentially requiring type arguments to derive from multiple types. Multiple interface types can be bound to a type parameter: But should be used with caution. Multiple interface bindings is usually a sign of a code smell (opens new window), suggesting that a new type should be created which acts as an adapter for the other types: |
| Implementing interfaces in an abstract class A method defined in an From Java 8 onward it is possible for an Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference