|
Posted by Jerry Stuckle on 07/23/07 17:37
Michael Fesser wrote:
> .oO(Jerry Stuckle)
>
>> Michael Fesser wrote:
>>> It's been a long time since I worked with Java and C++, but it looks
>>> like you're right in these cases. I came from the Pascal/Delphi world;
>>> in Object Pascal you have to explicitly call inherited constructors and
>>> destructors.
>> Object Pascal is not a true OO language. It's an OO extension to a
>> structured language (and not a real good implementation at that - there
>> are several holes in the way it is implemented). The ones I mentioned
>> are true OO languages.
>
> Depends on how you define "true OO language". Many people don't even see
> Java as a true OO language (mainly because of the scalar types IIRC).
> And when comparing C++ and Delphi, I clearly prefer the latter, its
> object model has some really nice and handy features (properties with
> read-only or write-only access for example, I like that).
>
> After all it all comes down to how strictly you want to follow a
> paradigm. It doesn't really matter if we're talking about OOP or MVC for
> example. Some people prefer to follow the rules as strictly as possible,
> but for me that would cause more troubles than it would solve. Sometimes
> you simply have to think more practical than theoretical.
>
>>> It would. A::doSomething() just has to check if $this->data is empty and
>>> react accordingly.
>> No, Micha, it is improper OO. The base class is depending on something
>> form the derived class.
>
> I still don't see it this way. The derived class just performs a
> different initialization of the data. The base class will still work
> with or without that, because it already has this method ifself. It's
> just overwritten by the child classes as necessary. I could even have
> declared A::doSomething() as abstract, so child classes would be forced
> to overwrite/implement it.
>
In a true OO system, the parent is initialized before the child. In
true OO languages such as Java and C++, this can be performed by passing
a parameter to the base class.
It's incorrect here because you 1) are not initializing the base class
first, and 2) are exposing the base class implementation.
Now what happens to the derived class(s) if you change the name of your
base class variable? Part of the OO design is the derived classes are
independent of the base class implementation.
> It's not always possible to initialize an object just by passing all the
> required data to the constructor, sometimes you have to perform
> additional task in separate methods. That's not a problem. And calling a
> child's method from a parent's method is not only allowed, but very
> common and important (virtual methods, polymorphism, things like that).
>
It is ALWAYS possible to initialize the base object via constructor
parameters in a properly designed OO system.
But we're not talking about polymorphism here. That's an entirely
different subject.
>> Additionally, in proper OO, A::data would be
>> private to the base class.
>
> Sure, can be done. But I prefer to declare my properties as protected,
> if they are supposed to be accessed in derived classes. I don't want to
> write getters and setters for every single member variable. While it
> would be "true OO", it's usually too much work for nothing. Things are a
> bit different when properties should be accessible from the outside, but
> even then I sometimes simply declare them as public.
>
Which is a classic example of overuse of "protected". Protected members
should be used rarely, if at all.
>> And the constructor for A would be called
>> before the constructor to B ever got started.
>
> Could be done easily in the modified version of my example.
>
> Micha
I suggest you read up on OO from the experts. Booch, Rumbaugh and
Stroustrup are good starters.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|