Interface in Java

Interface in Java

2 min read
  • Java does not support multiple inheritance, therefore, it provides a concept of inheritance for implementing multiple inheritance.
  • An interface same as an abstract class does not create an object of itself.
  • An interface is created using an interface keyword.
  • A static function has been implemented in an interface from JDK7.
  • An interface contains only public and abstract methods along with a static method.
  • An interface provides the only declaration of methods even public methods also except the static method.
  • A class can inherit an interface using the implements keyword. An interface can inherit another interface using the extends keyword. But it can not inherit the abstract class
  • When a class inherits an interface then their is mandatory to give the definition of all the methods of the interface except the static method.
  • Interface can’t have constructor.

Example:-

import java.util.Scanner;
interface India
 {
     public void currency1()//Abstract Method;
 }
 interface Russia
 {
     public void currency2();//Abstract Method       
}
 abstract class manager
 {
    abstract void travel();
 }
 class tour extends manager implements Russia,India
 {
     public void currency1()
     {
         System.out.println("The currency of India is rupees");
     }
     public void currency2()
     {
       System.out.println("The currency of Russia is Russian Ruble (1 russia ruble= 1.08 Rupees)");  
     }
     void travel()
     {
         System.out.println("Enter the country you want to travel");
         Scanner sc=new Scanner(System.in);
         String name=sc.next();
         if(name.equalsIgnoreCase("india"))
         {
             System.out.println("Charges are 1 lakh rs/-");
         }
         if(name.equalsIgnoreCase("russia"))
         {
             System.out.println("Charges are 2 lakh rubles/-");
         }
     }
     public static void main(String args[])
     {
         tour t=new tour();
         t.currency1();
         t.currency2():
         t.travel();
     }
 }

Output

Interface in Java Program

Tags:
Choose your Reaction!
Leave a Comment