Interface in java



Interface is a abstract class. It's means you can't define the method. interface support multiple inheritance. Interface is a only declare the method can not defined the method.








Example-1
interface Abc
{
        void show();
}

class A implements Abc
{
        public void show()
        {
              System.out.print("mycrazycoding");
        }
}

public class Main
{
        public static void main(String[] args) 
        {
              Abc obj = new A();
              
              obj.show();
        }
}


Output



mycrazycoding









In that example, To define the interface method using anonymous class




Example-2
interface Abc
{
         void show();
}
public class Main
{
         public static void main(String[] args) 
         {
                Abc obj = new Abc()
                {
                     public void show()
                     {
                            System.out.print("mycrazycoding");
                     }
                };

                obj.show();
         }
}


Output



mycrazycoding