I have created an interface Animal and make function makeSound. Now I implements Animal in Cat and Dog class and echo something from makeSound function. Now I am confused why I need to create interface while I do the same task only create Cat and Dog class. Let see an example
With Interface
interface Animal{
public function makeSound();
}
class Cat implements Animal{
public function makeSound(){
echo 'Meow';
}
}
class Dog implements Animal{
public function makeSound(){
echo 'Geow';
}
}
$cat = new Cat();
$cat->makeSound();
echo "<br>";
$dog = new Dog();
$dog->makeSound();
echo "<br>";
Without Interface
class Cat{
public function makeSound(){
echo 'Meow';
}
}
class Dog{
public function makeSound(){
echo 'Geow';
}
}
$cat = new Cat();
$cat->makeSound();
echo "<br>";
$dog = new Dog();
$dog->makeSound();
echo "<br>";