Using inheritance to efficiently represent various vehicle types : Inheritance « Class « PHP






Using inheritance to efficiently represent various vehicle types

 
<?
class Vehicle {
     var $model;
     var $current_speed;

     function setSpeed($mph) {
          $this->current_speed = $mph;
     }

     function getSpeed() {
          return $this->current_speed;
     }
}

class Auto extends Vehicle {
    var $fuel_type;

    function setFuelType($fuel) {
          $this->fuel_type = $fuel;
    }

     function getFuelType() {
          return $this->fuel_type;
     }

}

class Airplane extends Vehicle {
     var $wingspan;

     function setWingSpan($wingspan) {
          $this->wingspan = $wingspan;
     }

     function getWingSpan() {
          return $this->wingspan;
     }
}
?>
  
  








Related examples in the same category

1.subclass and parent class
2.Three levels of inheritance
3.extends and implement
4.Basic Inheritance
5.Class Inheritance
6.Class Member Binding in PHP
7.Creating a Class That Inherits from Another
8.Define an Executive class that inherits Employee
9.inheritance example
10.Using the extends keyword to define a subclass
11.Example of Inheritance
12.Overriding parent methods
13.Using the parent construct