java usage of interface and implementation code example
Example 1: how to implement a interface in java
interface methods{
public static hey();
}
class scratch implements methods{
public static hey(){
System.out.println("Hey");
}
}
Example 2: 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();
}
}