Why to use interfaces in Java
An interface is a Java programming language construct, similar to an abstract class that allows you to specify zero or more method signatures without providing the implementation of those methods. In OOP?s language such as Java, the object entities used to solve computing problems are thought to form a relation as of a tree, thus we have the class hierarchy. In a similar way, it is thought that interfaces can also form a hierarchy fruitfully. A good example is the list data type.
The interface is Java’s answer to multiple in heritance. It is a Java type that defines what should be done, but not how to do it. Interfaces are perhaps most useful when designing the API for your program.
In the Java programming language, an interface is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated?they can only be implemented by classes or extended by other interfaces. An interface will consist of method signatures that look like this; there can be no implementation at all. You do the implementation in a class
Using interfaces in JAVA allows you to substitute multiple implementations that conform to the interface (think plugins) while using a specific class hard-codes you to the class or its descendents. Interfaces also allow you to mimic multiple inheritance since classes can implement more than one interface but can only extend a single base class.
To use an interface in Java, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface.
There are really two ways that people in Java with term interface. One is conceptual and one is concrete.
People sometimes talk about a program’s interface, or even a class’ interface. Remember that API is an acronym for Application Programming Interface. When used this way, it means the thing with which we interact. It’s what is exposed to us that we can work with. It is the visible boundary of a class or a program or a programming language’s libraries. This is the conceptual version of the term interface. It means the public (I really mean non-private) methods that we can call to do something.
Java defines a few interfaces that are just used as a boolean property of a class, but don’t actually require the implementation of any methods. These are called tagging interfaces.
Tagging interfaces are an abuse of the interface principle, but Java makes use of object-oriented features to solve all problems if possible, rather than invent a more appropriate feature.
Common tagging interfaces that you might see are:
Implementing this interface indicates that the class has overridden Object’s clone () method. But there is no check that this is true, and because subclasses inherit interfaces, it will look like all subclasses have defined clone () altho that is not necessarily true. Yes, this is not the normal spelling of the English word. ??? Why needed.
This tagging interface indicates that a class can serialized – that an object of this class can be written out and read back in using ???. This can be useful for short-term storage of objects, but should not be used for long-term storage because any change to the class definition makes that persistent copy unreadable!
On the other hand, an interface is a Java programming language construct, similar to an abstract class that allows you to specify zero or more method signatures without providing the implementation of those methods. Remember the implementation is the code block that does the work.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.