1

Please consider the following code:

class baseclass {
    public $hideme;
    public function getit() { return $this->hideme; }
    public function setit($value) { $this->hideme = $value; }
}

class derived extends baseclass {
    private $hideme; //can't do this
    private function getit() { //can't do this
        return 0;
    }
}

function doobee(baseclass $obj) {
    echo $obj->getit() . "\n";
}

On the other hand in java language you can do that with properties. Also, even in php you can do the other way around, that is, a private property in the base class can be redefined as public in the child class, because it creates a new property in child class, instead of overriding. Why can't it do same when redeclaring public property as private in the child class?

This answer on SO says, The rationale being you shouldn't be able to hide members from the base class.... but, we are not hiding anything from base class!, base class's objects will still have access to those public properties and function. Its just the child classes' objects have either two versions(private and public) of same property or just the visibility be overridden?.

  1. What if I want just any grandchild classes inheriting from child(derived in code) class not have public properties of grandfather class($hideme in baseclass)?

  2. If it is a general rule of inheritance in oop as the SO answer suggest, why is not valid in java?

  3. What's up with the methods? #1 also applies to methods as well.

P.S: Please note that #1,#2 and #3 are not three different questions. Asking them as three different questions wouldn't make sense as they all circle around the same problem.

1 Answer 1

2

What if I want just any grandchild classes inheriting from child(derived in code) class not have public properties of grandfather class($hideme in baseclass)?

If you were able to change the visibility of a member of the base class, such that it would be less visible in the derived type...

Then, the grandchild class would NOT be usable as a replacement of its base classes. That is you would have inheritance but break Liskov Substitution Principle. That is not how we expect OOP inheritance to work.

If it is a general rule of inheritance in oop as the SO answer suggest, why is not valid in java?

In Java it isn't valid... You get an error that says attempting to assign weaker access privileges; was public.

Notice that Java does not have an override keyword. Because of that a method override and a new method look the same. The compiler decides if you are trying to override, if there is matching method in the base class.

You cannot remove or hide what the parent has. That would make the child unusable to substitute the parent. Thus, Java rejects this. By the way, C# would create a new method and issue a warning, which goes away with the new keyword.

Note: See the answer to the linked question.

What's up with the methods? #1 also applies to methods as well.

Yes, the idea of not hiding or removing members apply to any kind of members.


(...) in php (...) Why can't it do same when redeclaring public property as private in the child class?

PHP do not support multiple members with the same name.

On the other hand, Java does.

Java would do overload resolution to figure out, in context, which member are you calling.

In Java, you can have multiple methods with the same name, even in the same context, as long as they have different parameters. Then Java can (usually) tell which one you are calling by the parameters (when it can't you get an error saying that the call is ambiguous).

However, Java won't be able to distinguish two fields with the same name in the same context. Thankfully, the derived class is a different context from the base class. The code of the derived class can use the super keyword to refer to the members of the base class to which it has access.

By the way, PHP does not have "proper" overloading. Instead it has magic methods. Or rather I should PHP calls overloading to something different. See PHP: Overloading. See also PHP two methods with the same name.

5
  • I gotcha in the last point. what I am proposing in the question is equivalent to class child { /*public $hideme; //invisibly the same thing*/ private $hideme; }. It is just same as having $hideme declared twice with different visibility -- php doesn't allow that; but may be as you say java let you do that.
    – user106313
    Commented Jan 27, 2020 at 7:47
  • @user106313 One approach to think about inheritance is as reusing everything of the base class. I added note to the answer about two fields with the same name in Java.
    – Theraot
    Commented Jan 27, 2020 at 7:59
  • In java's override explanation, did you mean to say change the visibility of the property? Because when you say The inherited one still exist, and is still usable. it sounds contradictory, because this says inherited method overrides the parent method, and hence the OP was getting error.
    – user106313
    Commented Jan 27, 2020 at 8:00
  • @user106313 Ah, I see, I got that wrong. That was me thinkin C#.
    – Theraot
    Commented Jan 27, 2020 at 8:04
  • That's fine. Thanks for your help. I got the gist of it anyway. I don't know java, but I see the concept behind php's design in that regard. I'll wait for a while for other answers before accepting yours.
    – user106313
    Commented Jan 27, 2020 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.