Java Interface


Interfaces

Another way to achieve Java releases, is to use connectivity.

An interface is a completely "abstract class" that is used to group related methods with empty bodies:


Example
// interface
            interface Animal {
              public void animalSound(); // interface method (does not have a body)
              public void run(); // interface method (does not have a body)
            }
            

In order to access interactive methods, the interface must be "used" (as if inherited) another class with the implements keyword (instead of extends). The interface of the virtual interface is given the "use" category:


Example
// Interface
            interface Animal {
              public void animalSound(); // interface method (does not have a body)
              public void sleep(); // interface method (does not have a body)
            }
            
            // Pig "implements" the Animal interface
            class Pig implements Animal {
              public void animalSound() {
                // The body of animalSound() is provided here
                System.out.println("The pig says: wee wee");
              }
              public void sleep() {
                // The body of sleep() is provided here
                System.out.println("Zzz");
              }
            }
            
            class Main {
              public static void main(String[] args) {
                Pig myPig = new Pig();  // Create a Pig object
                myPig.animalSound();
                myPig.sleep();
              }
            }
            

Notes on Interfaces:

  • Like abstract classes, interactive links cannot be used to create objects (in the example above, it is not possible to create an "Animal" item in MyMainClass)
  • Interface methods do not have a body - the body is given a "use" category.
  • In using the interface, you have to uninstall all the options
  • The automatic interface modes are abstract and public
  • Automatic visual interface attributes are public, static and final
  • The interface cannot contain a builder (as it cannot be used to create objects)
Why And When To Use Interfaces?
  1. For security - hide certain details and display important item details (visible connector).
  2. Java does not support "multiple assets" (class can inherit from one superclass). However, it can be accessed through interfaces, as the class can implement multiple connections.

Note: To use multiple interaction links, separate them with commas (see example below).


Multiple Interfaces

To use multiple interaction links, break them up with commas:


Example
interface FirstInterface {
            public void myMethod(); // interface method
          }
          
          interface SecondInterface {
            public void myOtherMethod(); // interface method
          }
          
          class DemoClass implements FirstInterface, SecondInterface {
            public void myMethod() {
              System.out.println("Some text..");
            }
            public void myOtherMethod() {
              System.out.println("Some other text...");
            }
          }
          
          class Main {
            public static void main(String[] args) {
              DemoClass myObj = new DemoClass();
              myObj.myMethod();
              myObj.myOtherMethod();
            }
          }