Inheritance & Visibility

Inheritance & Visibility

I managed to keep my last post about inheritance relatively short, and I’m going to do the same for this post. I think it’s easier to digest little bits of information at a time. This post will cover a little bit more about inheritance and also visibility.

Inheritance

First, we will continue learning more about inheritance.

Abstract Classes

An abstract class is a class that can be extended, but not instantiated. So, we may not want people to instantiate the Person class and require they instantiate a class that extends the Person class, like the Boy class. Below is an example of the Person class without the guts.


<?php
abstract class Person {
    // . . .
    // code 
    // . . .
}

class Boy extends Person {
    // . . .
    // code
    // . . .
}

$person = new Person("Levi", 32); // Won't work.
$boy = new Boy("Tahm", 4); // Will work.

Don’t get abstract classes confused with interfaces. Abstract classes can contain the guts of a method; Interfaces can only define the name/parameters of the methods.

Final Classes

You can not extend a final class. Final classes can be useful when you want to put some restriction on the extension of a class. You can also create a final method in any class so that no one can override it. Below is an example of a final class and a final method.


<?php
abstract class Person {
    // . . .
    // code from previous posts
    // . . .

    // This method can not be overridden.
    final public function wink() {
        echo ";)";
    }
}

final class Boy extends Person {
    // . . .
    // code
    // . . .
}

class Kiddo extends Boy {
    // This will not work. You will get an error.
}

Visibility

So far we have made everything public, which is fine while we learn. I wouldn’t mind having invisibility as a super hero power.

Public

Public indicates that you can access it directly after instantiating the class as shown below.


<?php
$boy = new Boy("Tahm", 4);
$boy->age = 5; // We were able to change this property directly.
$boy->eat('Pizza'); // We were also able to call this public method.

Protected

Protected restricts the usage to the inside of the class. It also allows other classes that extend the class that declared the property or method as protected to access it. That is confusing, and I hope the example below helps.


<?php
class Person {
    protected $age;
}

class Boy extends Person {
    public function getAge() {
        echo $this->age;
    }

    public function setAge($age) {
        $this->age = $age;
        $this->status();
    }

    protected function status() {
        echo "Updating the property.";
    }
}

$boy = new Boy("Tahm", 4);
$boy->age = 5; // This will not work. You will get an error.
$boy->getAge(); // OUTPUT: 4

// This will work, since setAge is a public method.
$boy->setAge(5); // OUTPUT: Updating the property.
$boy->getAge(); // OUTPUT: 5

That example covered public and protected methods. It also covered protected properties. We weren’t able to alter the age property directly, but we were able to change it by using the public method setAge(). When we used the setAge() method, it called the protected method status(), which output a message for us.

So anything public is like living in the “Wild Wild West” with Will Smith. You can set it, call it, and update it. Anything protected can only be used by that class or any class that extends it.

Private

You can only use a private method or property in the class that initially declared it. Classes that extend that class can not use it. You can not update it or use it outside of that class. Below is an example of a private method and property.


<?php
class Dog {
    private $owner;
    public function setOwner($owner) {
        $this->owner = $owner;
        $this->walk();
    }

    private function walk() {
        echo "I love being walked by " . $owner;
    }
}

class Rover extends Dog {
    public function hello() {
        echo $owner; // This won't work. Owner is private.
        $this->walk(); // This won't work either.
    }
}

$corky = new Dog;
$corky->setOwner("Levi"); // OUTPUT: I love being walked by Levi
$corky->walk(); // This won't work.

Okay, this post turned out to be longer than I originally planned. Maybe I shouldn’t have used a Will Smith movie as a reference. Send me an email if you have any questions or if I have any typos.

Will Smith is an amazing actor. I’m an expert on dogs and people.