Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:51:34+00:00 2026-05-16T09:51:34+00:00

So, this is the final nagging inheritance question I’ve had for a little bit

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:51:35+00:00Added an answer on May 16, 2026 at 9:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Final question for the night. And apologies for the complete noobness of this. I
In java, I could do this with the 'final' keyword. I don't see 'final'
So the final menu will look something like this: Item B Item B-1 Item
I've seen examples like this: public class MaxSeconds { public static final int MAX_SECONDS
My application links against libsamplerate.a. I am doing this to make distributing the final
This is a bit of a long shot, but if anyone can figure it
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
I have a class, which I have simplified to this: final class Thing {
This is a follow up to another question of mine. The solution I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.