Interfaces Flashcards
Interface
an interface is used as a contract to guarantee implementation of certain behaviors in a subclass.It’s an abstract entity - all methods are implicitly “public abstract” while instance variables, if there are any, are “public static final”.
To implement an interface use
the implement keyword
Interface methods and variables
- All methods are public abstract
- all variables are public static final
Interfaces and Inheritance
- said to have “type” inheritance
- a class that implements an interface is polymorphically considered an instance of the interface
- An interface describes what behaviors a class should have but provides none of its own.
Class and interfaces
-A class can implement multiple interfaces but can only extend one class
Java 8 Interfaces
Public-facing interfaces (APIs) were difficult to update or change. So default methods were created.
Default methods
-interface methods that are implemented in the interface (Because an implementation is present in the interface, classes that implement that interface are not required to implement the method. To use a default method, you just add the default keyword before the method declaration in the interface.Then you can provide an implementation, like normal.) -implementing classes are not required to implement default methods, but they can override them -the guarantee with an interface is that the implementing class will provide an implementation for those other methods, so they are safe to call.Interfaces are also allowed to have static methods with definitions.Like any other static method, you can invoke these without creating an instance of a class that implements the interface.And because they are static methods, they can only call other static methods, or access methods through local reference variable
static methods
- can be called without creating an instance of the class
- can only call other static methods directly. Must use local reference variables to call non-static methods
Multiple Inheritance
Use the super() keyword to refer to a specific interface method (in the case that two interfaces have default methods with the same name)
Abstract Classes
- Can have instance variables that are not always public, static, final
- can have methods that are bot public
What is the difference between interfaces vs abstract classes?*
- A class can extend only one class (abstract or not) but can implement multiple interfaces
- Interface variables are implicitly public static final. Abstract classes can declare any modifiers on instance variables.
- Interfaces can (as of Java 8) provide default methods, but all others are public abstract. Abstract classes can declare any modifiers on their methods.
Why use an interface over an abstract class?
- In general, if you wanted to develop and enforce a certain hierarchy of functionality where state and behavior was tracked, then you would use an abstract class
- If you want a class to be free to implement functionality without constrained to a hierarchy, then an interface is a better approach.