Important Points about Interface in Java

参考

Interface in Java.


Important Points about Interface in Java

  1. interface is the code that is used to create an interface in java.

  2. We can’t instantiate an interface in java.

  3. Interfaces can’t have constructors because we can’t instantiate them and interfaces can’t have a method with body.

  4. Interface provides absolute abstraction, abstract classes in java to provide abstraction but abstract classes can have method implementations but interface can’t.

  5. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.

  6. 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.

  7. By default interface methods are implicitly public and abstract, it makes total sense because the method don’t have body and so that subclasses can provide the method implementation.

  8. 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.

  9. implements keyword is used by classes to implement an interface.

  10. A class implementing an interface must provide implementation for all of its method unless it’s an abstract class.

  11. 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.

你可能感兴趣的:(基础知识,java,开发语言)