Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good or Bad OOP? [closed]

Assume there is an abstract class with a constructor that calls a protected abstract method that is yet to be implemented by the child class. Is this a good or bad idea? Why?

like image 359
StackOverflowNewbie Avatar asked Nov 03 '10 00:11

StackOverflowNewbie


1 Answers

This is a bad idea.

You're basically creating inversion of control within a constructor. The method in the base class that is being called gets called before the base class data is initialized (in most languages), which is dangerous, as well. It can easily lead to indeterminate behavior.

Remember, in most languages, when you construct a class, all of the base class construction runs first. So, if you have something like: MyClass() : MyBaseClass() {}, typically, MyBaseClass's constructor runs in its entirety, then MyClass's constructor executes. However, by using a virtual method in the base class, you're calling an instance method in MyClass before it's fully initialized - which could be very dangerous.

like image 186
Reed Copsey Avatar answered Oct 08 '22 21:10

Reed Copsey