How do I get a PHP class constructor to call its parent’s parent’s constructor?

I need to have a class constructor in PHP call its parent’s parent’s (grandparent?) constructor without calling the parent constructor. // main class that everything inherits class Grandpa { public function __construct() { } } class Papa extends Grandpa { public function __construct() { // call Grandpa’s constructor parent::__construct(); } } class Kiddo extends Papa … Read more

Do subclasses inherit private fields?

This is an interview question. Does subclasses inherit private fields? I answered “No”, because we can’t access them using the “normal OOP way”. But the interviewer thinks that they are inherited, because we can access such fields indirectly or using reflection and they still exist in the object. After I came back, I found the … Read more

Calling parent class __init__ with multiple inheritance, what’s the right way?

Say I have a multiple inheritance scenario: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What’s the right code to write here to ensure # A.__init__ and B.__init__ get called? There’s two typical approaches to writing C‘s __init__: (old-style) ParentClass.__init__(self) (newer-style) super(DerivedClass, self).__init__() … Read more

How to define custom exception class in Java, the easiest way?

I’m trying to define my own exception class the easiest way, and this is what I’m getting: public class MyException extends Exception {} public class Foo { public bar() throws MyException { throw new MyException(“try again please”); } } This is what Java compiler says: cannot find symbol: constructor MyException(java.lang.String) I had a feeling that … Read more

Benefits of prototypal inheritance over classical?

So I finally stopped dragging my feet all these years and decided to learn JavaScript “properly”. One of the most head-scratching elements of the languages design is its implementation of inheritance. Having experience in Ruby, I was really happy to see closures and dynamic typing; but for the life of me can’t figure out what … Read more