java what is a function interface code example

Example 1: why we use interface in java

interface Animal {
   void child();
}
class Cat implements Animal {
   public void child() {
      System.out.println("kitten");
   }
}
class Dog implements Animal {
   public void child() {
      System.out.println("puppy");
   }
}
public class LooseCoupling{
   public static void main(String args[]) {
      Animal obj = new Cat();
      obj.child();
   }
}

Example 2: java define interface

/* File name : Animal.java */
interface Animal {
   public void eat();
   public void travel();
}

Tags:

Java Example