interface in java\ code example
Example 1: Interface in java
interface Vehicle
{
public void accelerate();
}
class BMW implements Vehicle
{
public void accelerate()
{
System.out.println("BMW accelerating...");
}
}
public class InterfaceDemo
{
public static void main(String[] args)
{
BMW obj = new BMW();
obj.accelerate();
}
}
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();
}
}