Interfaces and functional interface in Java – tutorial
This simple sumarize informationa about interfances and funcional interfnces in Java. Interfances An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants,
This simple sumarize informationa about interfances and funcional interfnces in Java.
Interfances
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
An interface is similar to a class in the following ways −
- An interface can contain any number of methods.
- An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
- The byte code of an interface appears in a .class file.
- Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
However, an interface is different from a class in several ways, including −
- You cannot instantiate an interface.
- An interface does not contain any constructors.
- All of the methods in an interface are abstract.
- An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
Declaring Interfaces
The interface keyword is used to declare an interface. Here is a simple example to declare an interface −
First install https://gradle.org/install/ the best way is use SDKMAN! Simply open a new terminal and enter:
$ curl -s "https://get.sdkman.io" | bash
Follow the instructions on-screen to complete installation. Next, open a new terminal or enter:
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
Lastly, run the following code snippet to ensure that installation succeeded:
$ sdk version
Following is an example of an interface :
/* File name : NameOfInterface.java */ import java.lang.*; // Any number of import statements public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations }
Interface in Java
- Introduction to Interface
- Multiple Inheritance – Example
- Why Interfaces are needed
- Java’s Interface Concept
- Syntax
- Semantic Rules for Interfaces
- Example: An Interface for Shape Classes
- Extending Interface
- Abstract class and Interface
- Benefits of Interfaces
- Java’s Most used Interfaces
Interface in Java
ABSTRACT CLASS & INTERFACE
- What is interface?
- How to define interface?
- How to use interface?
- Why not use abstract class instead of interface?
- UML of interface
- Importance of interface
- Different between interface and abstract class
- Notes for interface
- Class Design Guidline
- Last words
Â
Â