https://www.tutorialspoint.com/java/java_interfaces.htm
http://www.journaldev.com/1601/interface-in-java
Interface in java provide a way to achieve abstraction. Java interface is also used to define the contract for the subclasses to implement.
Differences between interface and abstract class
http://www.journaldev.com/1607/difference-between-abstract-class-and-interface-in-java
- interface is the code that is used to create an interface in java.
- We can’t instantiate an interface in java.
- Interface provides absolute abstraction, in last post we learned about abstract classes in java to provide abstraction but abstract classes can have method implementations but interface can’t.
- Interfaces can’t have constructors because we can’t instantiate them and interfaces can’t have a method with body.
- By default any attribute of interface is public, static and final, so we don’t need to provide access modifiers to the attributes but if we do, compiler doesn’t complain about it either.
- By default interface methods are implicitly abstract and public, it makes total sense because the method don’t have body and so that subclasses can provide the method implementation.
- An interface can’t extend any class but it can extend another interface. public interface Shape extends Cloneable{} is an example of an interface extending another interface. Actually java provides multiple inheritance in interfaces, what is means is that an interface can extend multiple interfaces.
- implements keyword is used by classes to implement an interface.
- A class implementing an interface must provide implementation for all of its method unless it’s an abstract class.
- We should always try to write programs in terms of interfaces rather than implementations so that we know beforehand that implementation classes will always provide the implementation and in future if any better implementation arrives, we can switch to that easily.