Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,817 answers

573 users

How to use polymorphism interface and method overriding PHP

3 Answers

0 votes
interface Animal {
    public function Say();
}

class Dog implements Animal {
    public function Say()
    {
        echo '(Dog) Woof! Woof!';
    }
}

class Cat implements Animal {

    public function CatSay() // Error
    {
        echo '(Cat) Meow! Meow!';
    }
}

$dog = new Dog();
$dog->Say();

 
/*
run: 
 
Fatal error: Class Cat contains 1 abstract method and must therefore be 
declared abstract or implement the remaining methods (Animal::Say) 
in C:\xampp\htdocs\allonpage.com\test.php on line 22 
  
*/ 

 



answered Sep 8, 2017 by avibootz
0 votes
interface Animal {
    public function Say();
}

class Dog implements Animal {
    public function Say()
    {
        echo '(Dog) Woof! Woof!';
    }
}

class Cat implements Animal {

    public function f() 
    {
        echo '(Cat) f()';
    }
}

$dog = new Dog();
$dog->Say();

 
/*
run: 
 
Fatal error: Class Cat contains 1 abstract method and must therefore be 
declared abstract or implement the remaining methods (Animal::Say) 
in C:\xampp\htdocs\allonpage.com\test.php on line 20
  
*/

 



answered Sep 8, 2017 by avibootz
0 votes
interface Animal {
    public function Say();
}

class Dog implements Animal {
    public function Say()
    {
        echo '(Dog) Woof! Woof!';
    }
}

class Cat implements Animal {

    public function Say()
    {
        echo '(Cat) Meow! Meow!';
    }
}

$dog = new Dog();
$dog->Say();

echo "<br />";

$cat = new Cat();
$cat->Say();

 
/*
run: 
 
(Dog) Woof! Woof!
(Cat) Meow! Meow! 
  
*/  

 



answered Sep 8, 2017 by avibootz

Related questions

1 answer 137 views
137 views asked Jan 4, 2022 by avibootz
1 answer 161 views
1 answer 122 views
1 answer 180 views
1 answer 158 views
1 answer 186 views
...