I've encountered with a bit strange issue which breaks my understanding of basic OOP principles.
In short, there are two classes: parent (virutal) and child (extends parent). Both of them have private method with the same name and signature. This private method is called right from the constructors. When instance of child class is created, private method from child class is called twice, and no call to the method from the parent class.
Bellow is the snippet of code to reproduce this behaviour: two classes and debug log output.
Parent class:
public without sharing virtual class BoomParentClass {
public BoomParentClass() {
System.debug('XXX: Hello from BoomParentClass constructor');
init();
}
private void init() {
System.debug('XXX: Hello from BoomParentClass init method');
}
}
Child class:
public without sharing virtual class BoomChildClass extends BoomParentClass {
public BoomChildClass() {
System.debug('XXX: Hello from BoomChildClass constructor');
init();
}
private void init() {
System.debug('XXX: Hello from BoomChildClass init method');
}
}
The command I'm executing:
new BoomChildClass();
Debug log outputs:
14:00:30:094 USER_DEBUG [6]|DEBUG|XXX: Hello from BoomParentClass constructor
14:00:30:094 USER_DEBUG [15]|DEBUG|XXX: Hello from BoomChildClass init method
14:00:30:094 USER_DEBUG [6]|DEBUG|XXX: Hello from BoomChildClass constructor
14:00:30:094 USER_DEBUG [15]|DEBUG|XXX: Hello from BoomChildClass init method
Why is it like that ? I thought private method belongs to the class itself and can't be overriden (that's why they are called 'private'). Any help would be appreciated.