JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Interface in Java

In Java, an interface is an abstract type that contains a collection of methods and constant variables. It is one of the core concepts in Java and is used to achieve abstraction, polymorphism and multiple inheritances.

How to declare an interface in java?

An interface is declared by using the interface keyword. A class that implements an interface must implement all the methods that are declared in the interface. It provides total abstraction, that means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default.

Syntax:

interface InterfaceName{  

    // declare constant fields  
    // declare methods that abstract   
    // by default.  

}

Why we use interface?

1. It is used to achieve total abstraction.

2. It can be used to achieve loose coupling.

3. By interface, we can support the functionality of multiple inheritance.

4. To achieve security - hide certain details and only show the important details of an object (interface).

Example of an interface in Java:

interface Bicycle {
    void changeCadence(int newValue); // interface method (does not have a body) 
    void changeGear(int newValue); // interface method (does not have a body)
    void speedUp(int increment); // interface method (does not have a body)
    void applyBrakes(int decrement); // interface method (does not have a body)
}

To access the interface methods, the interface must be implemented by another class

To implement an interface we use keyword implements

interface ShowMessage{  
    void message();  
    }  
    class Simple implements ShowMessage{  
    public void message(){System.out.println("Hello Programmer");}  
      
    public static void main(String args[]){  
    Simple obj = new Simple();  
    obj.message();  
     }  
}  

Output:

Hello Programmer

Example of AgeCalculator

import java.util.Scanner;

interface AgeCalculator
{
    public int currentAge();
}

class Interface implements AgeCalculator
{
    int dob, cyear;
    String name;
    Interface(String n, int cy, int d)
    {
        name = n;
        cyear = cy;
        dob = d;
    }

    public int currentAge()
    {
        return (cyear - dob);
    }
    void displayAge()
    {
        System.out.println(name+" your current age is "+currentAge());
    }
    public static void main(String agre[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name : ");
        String name = sc.nextLine();
        System.out.println("Enter current year : ");
        int cyear = sc.nextInt();
        System.out.println("Enter your DOB year : ");
        int dob = sc.nextInt();
        Interface p = new Interface(name,cyear,dob);
        p.displayAge();
    }
}

Output:

Enter your name :
Stark
Enter current year :
2021
Enter your DOB year :
1999
Stark your current age is 22

Multiple inheritance in Java by interface

To implementing multiple inheritance we use interface.

interface A{  
    void print();  
}  
interface B{  
    void show();  
}  
class Simple implements A,B{  
    public void print()
    {
        System.out.println("Hello");
    }

    public void show()
    {
        System.out.println("Programmer");
    }  
        
    public static void main(String args[]){  
    Simple obj = new Simple();  
    obj.print();  
    obj.show();  
        }  
} 

Output:

Hello
Programmer