Interface - Java

  • Software Architecture
    • Design Pattern
    • UI Designers
    • Software Design
  • Modeling
    • Interface is used for this
    • Languages
      • Java IDL (Interface Definition Language)
      • CORBA (Common Object Request Broker Architecture) => Component Based Reusable Items, Used in Networking
      • Java RMI (Remote Method Invocation)
      • Objective C, xcode => MVC (Model View Controller) => Model is interface.java, View is App.java, Controller is BusinessLogic.java
  • Interface
    • No implementation, Only Signature
    • implements => This keyword is used
    • Properties/Fields are public, static, final
    • Methods are public
    • Multiple Inheritance allowed => A class can implement multiple Interfaces
    • Inheritance for Interface
  • Declaring a Interface, Can also declare default methods
        public interface interfaceName {
            // Abstract Method
            void functionName(parameters);
    
            // Default Method with Implementation
            default void functionName(parameters) {
            // Statements
            }
        }
    
  • Extending a Interface, Methods will get overridden but will have their own initialization
         interface interfaceName2 extends interfaceName1 {}
    
  • Have to define all the methods of interfaceName in the Class
        class ClassName implements interfaceName {
            void functionName(parameters) {
            // Statements
            }
        }
    
  • Multiple Inheritance, Have to define all the methods of interfaceName1 & interfaceName2 in the Class
        class ClassName implements interfaceName1, interfaceName2 {
            // Statements
        }
    
  • Default Methods
    • Add implementation to methods of the interface without needing to overwrite it in the class which implements it
          public interface VarInterface {
              public default void print() {
                  System.out.println(“Namaste!”);
              }
          }
      
  • Static Methods
    • Can’t be overridden by implemented class unlike Default methods
    • Invoke using interface name => VarInterface.print();
          public interface VarInterface {
              static void print() {
                  System.out.println(“Namaste!”);
              }
          }
      
  • Anonymous Inner Classes
    • Declare & instantiate a class at the same time
    • Do not have a name, used only once
    • Code
          public interface Greeting {public void greet(String name);}
          class WelcomeUser {
              Greeting greetingObject = new Greeting() {
                  public void greet(String name) {
                      System.out.println("Hello" + name);
                  }
              };
          }
      
  • Functional Interface
    • Interface with only one abstract method
    • It is annotated with @FunctionalInterface. Although it is not compulsory
          @FunctionalInterface
          public interface MyFunctionalInterface {
              String sayHello(String name);
          }
      
    • Can add any number of default & static methods
    • Can have public methods of Object class as abstract
  • Lambda expression
    • Can be passed to an function just like an object
    • Can be used only with functional interfaces
    • Code
          () -> expression
          (parameter1, parameter2) -> expression
          parameter -> expression
          (parameters) -> {statements;}
      
    • Local Variable => Used for parameter declaration for lambda expressions
      • var
Share: