OOP PHP 4/8 Overriding Parent Methods - Sorry no PHP category


#1

I am having difficulty understanding why parts of this are neccessary, or at least trouble understanding any direct link and what makes one class a child or subclass or parent of another?

<?php
        class Vehicle {
          public function honk() {
            return "HONK HONK!";
          }
        }

        class Bicycle extends Vehicle {
            public function honk() {
             return "Beep beep!";   
            }
        }
      ?>

I can do it and make it correctly, but i don’t know what links the “Bicycle” class to it’s so-called parent class “Vehicle”.

      <?php
        class Vehicle {
          public function honk() {
            return "HONK HONK!";
          }
        }

        class Bicycle {
            public function honk() {
             return "Beep beep!";   
            }
        }
      ?>

This is incorrect but the output is the exact same, what’s stopping me from just doing this?

EDIT: Slowly played around and figured that “extends” appears to be what makes classes linked into parents and children, having an empty child class(Bicycle) still inherits properties of parent, “extends” is the link!


#2

yep, the confusing thing here is that you overwrote the only method the parent class has, so it doesn’t seem very effective. However, inheritance is a powerful tool.


#3

I’m a bit late but you guessed it correctly, however, as @stetim94 said, you overwrote the same method of the parent class.

A good example can be found here.