views:

64

answers:

3

So, this is the final nagging inheritance question I've had for a little bit so I wanted to go on ahead and ask. So I'll give an example in PHP:

<?php

class Base
{
    private $z = 4;


    function GetPrivate()
    {
        echo $this->z;
    }

}

class Derived extends Base
{
}

$b = new Base();
$d = new Derived();

$d->GetPrivate();

?>

Simple enough. When I have always read about inheritance, the explanation were simply just "you inherit the public and protected members" and that's it. What I don't get are a couple things about how the interpreter in this example figures what belongs to what.

For example, when create a derived class, I am able to use the public function "GetPrivate" of the Base get the base class's private variables. However, the simple definition of inheritance doesn't work with this to me. What I mean is, I inherit the GetPrivate method but am still have some sort of link to private variables just from that method which belonged to the base class (even though $this refers to the derived class object). I couldn't create a new function in the Derived class to access those private variables.

Thus, does the interpreter keep tabs on what were the inherited functions from the base class and the possible links they hold to private members only available to that base class?

+1  A: 

The answer is a simple yes, try this out:

<?php


class Base
{
    private $z = 10;

    public function getPrivate()
    {
        return $this->z;
    }
}


class Derived extends Base
{
    public function getPrivate()
    {
        return $this->z;
    }
}

$a = new Derived();

echo $a->getPrivate();

You will see that now that we have defined getPrivate on the Derived class we can no longer access z in the Base since its private, if we want to be able to access it from the derived class we need to declare it to be protected instead of private.

Kristoffer S Hansen
+1  A: 

The interpreter (or compiler in other OOP language), check the access one step at a time.

When you call $d->GetPrivate();, the interpreter check the context in this is main (public context as I assume that you are not in a related class to Drerived or Base) and GetPrivate() is a public method. Thus, $d->GetPrivate(); is allowed in this context so no error.

In GetPrivate(), the context is $d object as Base and the access to z is a private element of the current object ($d). Thus the access is valid.

The concept that comes in to play here is 'Data Hiding' (access control) and 'Encapsulation' (combination of data and function).

Inheritance of to play only to allows GetPrivate() of Base to be used as it belong to an object of Derived.

It is true that there is still a link to a private Data but that link is not a direct one. The importance is that the access happen as Base class allowed.

So to answer your question is:

YES! The interpreter keep tabs on what were the inherited functions from the base class and the possible links they hold to private members only available to that base class.

Hope this helps.

NawaMan
A: 

Well, I cannot tell much about the parser details, but I am sure Artefacto could shed some light on that. But anyway, the key to understanding is in understanding what visibility means:

Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defined the member.

Now, the PHP manual also states:

For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

If you do a var_dump($d) on the derived class, you will see it contains Base->z:

object(Derived)#2 (1) {
  ["z":"Base":private]=>
  int(4)
}

So there is a reference to z in Base, but it is private and since private means the member may only be accessed by the class that defines the member, you cannot access it from Derived.

By offering a public methods in Base to get to the private member, you are effectively controlling access through the parent method. Maybe $z is something that has to be read-only in extended classes, for instance a database adapter.

This is information hiding and access control. It doesnt mean, if you extend Base, you will lose $z. Inheritance is an is-a relationship. Derived is-a Base and as such, it has a $z, although not on itself but through it's parent.

Gordon