Objects get really interesting when we start using inheritance. Using this technique, we can create classes — known as child classes — that are based on another class: the parent class. A child class inherits all the properties and methods of its parent, and it can also add additional properties and methods.
The wonderful thing about inheritance is that, if we want to create a lot of similar classes, we have to write the code that they have in common only once, in the parent class. This saves us from duplicating code. Furthermore, any outside code that can work with the parent class automatically has the ability to work with its child classes, provided the code works only with the properties and methods contained in the parent class.
Imagine that we're creating a program to deal with various regular shapes, such as circles, squares, equilateral triangles, and so on. We want to create a Shape class that can store information such as number of sides, side length, radius, and color, and that can calculate values such as the shape's area and perimeter. However, not all shapes are the same. Circles don't really have a clearly defined number of sides, and we calculate an equilateral triangle's area using a different formula than for a square. So if we wanted to handle all types of regular shapes in a single Shape class, our class's code would get quite complicated.
By using inheritance, however, we can break the problem down into simpler steps. First, we create a parent Shape class that contains just those properties and methods that are common to all shapes. Then, we can create child classes such as Circle, Square, and Triangle that inherit from the Shape class.
The wonderful thing about inheritance is that, if we want to create a lot of similar classes, we have to write the code that they have in common only once, in the parent class. This saves us from duplicating code. Furthermore, any outside code that can work with the parent class automatically has the ability to work with its child classes, provided the code works only with the properties and methods contained in the parent class.
Imagine that we're creating a program to deal with various regular shapes, such as circles, squares, equilateral triangles, and so on. We want to create a Shape class that can store information such as number of sides, side length, radius, and color, and that can calculate values such as the shape's area and perimeter. However, not all shapes are the same. Circles don't really have a clearly defined number of sides, and we calculate an equilateral triangle's area using a different formula than for a square. So if we wanted to handle all types of regular shapes in a single Shape class, our class's code would get quite complicated.
By using inheritance, however, we can break the problem down into simpler steps. First, we create a parent Shape class that contains just those properties and methods that are common to all shapes. Then, we can create child classes such as Circle, Square, and Triangle that inherit from the Shape class.
To create a child class that's based on a parent class, we use the extends keyword, as follows:
class Shape
{
// (General Shape properties and methods here)
}
class Circle extends Shape
{
// (Circle-specific properties and methods here)
}
The following script shows inheritance in action. It creates a parent Shape class, holding properties and methods common to all shapes, then creates two child classes based on Shape — Circle and Square — that contain properties and methods related to circles and squares, respectively.
<?php
class Shape {
private $_color = "black";
private $_filled = false;
public function getColor() {
return $this->_color;
}
public function setColor( $color ) {
$this->_color = $color;
}
public function isFilled() {
return $this->_filled;
}
public function fill() {
$this->_filled = true;
}
public function makeHollow() {
$this->_filled = false;
}
}
class Circle extends Shape {
private $_radius = 0;
public function getRadius() {
return $this->_radius;
}
public function setRadius( $radius ) {
$this->_radius = $radius;
}
public function getArea() {
return M_PI * pow( $this->_radius, 2 );
}
}
class Square extends Shape {
private $_sideLength = 0;
public function getSideLength() {
return $this->_sideLength;
}
public function setSideLength( $length ) {
$this->_sideLength = $length;
}
public function getArea() {
return pow( $this->_sideLength, 2 );
}
}
$myCircle = new Circle;
$myCircle->setColor( "red" );
$myCircle->fill();
$myCircle->setRadius( 4 );
echo "<h2>My Circle</h2>";
echo "<p>My circle has a radius of " . $myCircle->getRadius() . ".</p>";
echo "<p>It is " . $myCircle->getColor() . " and it is " . ( $myCircle->isFilled() ? "filled" : "hollow" ) . ".</p>";
echo "<p>The area of my circle is: " . $myCircle->getArea() . ".</p>";
$mySquare = new Square;
$mySquare->setColor( "green" );
$mySquare->makeHollow();
$mySquare->setSideLength( 3 );
echo "<h2>My Square</h2>";
echo "<p>My square has a side length of " . $mySquare->getSideLength() . ".</p>";
echo "<p>It is " . $mySquare->getColor() . " and it is " . ( $mySquare->
isFilled() ? "filled" : "hollow" ) . ".</p>";
echo "<p>The area of my square is: " . $mySquare->getArea() . ".</p>";
?>
How It Works
The script first creates the parent Shape class. This class contains just the properties and methods common to all shapes. It contains private properties to store the shape's color and record whether the shape is filled or hollow, then provides public accessor methods to get and set the color, as well as fill the shape or make it hollow and retrieve the shape's fill status.
Next, the script creates a Circle class that inherits from the Shape class. Remember that a child class inherits all the properties and methods of its parent. The Circle class also adds a private property to store the circle's radius, and provides public methods to get and set the radius, as well as calculate the area from the radius using the formula πr2.
The script then creates Square, another class that inherits from Shape. This time, the class adds a private property to track the length of one side of the square, and provides methods to get and set the side length and calculate the square's area using the formula (side length)2.
Finally, the script demonstrates the use of the Circle and Square classes. First it creates a new Circle object, sets its color, fills it, and sets its radius to 4. It then displays all the properties of the circle, and calculates its area using the getArea() method of the Circle class. Notice how the script calls some methods that are in the parent Shape class, such as setColor() and isFilled(), and some methods that are in the child Circle class, such as setRadius() and getArea().
The script then repeats the process with the Square class, creating a hollow green square with a side length of 3, then displaying the square's properties and calculating its area using the Square class's getArea() method.
No comments:
Post a Comment