Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il

Hostgator - Save 50% Off Your New Optimized WordPress Hosting With Coupon: WDA50

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Dedicated Hosting Superb Internet

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,585 questions

9,406 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 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 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 by avibootz
...