[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: protected vs private (was Re: kdeui library inconsistency in   knewpanner.h)
From:       "P.J.Leonard" <P.J.Leonard () bath ! ac ! uk>
Date:       1998-02-24 12:56:41
[Download RAW message or body]

Stefan Taferner wrote:

[snip]

> > IMHO private is better than protected because it makes the base class
> >more encapsulated.
> 
> If you write a class, and you encapsulate everything very strict (most methods
> private instead of protected), you have to be really good and foreseeing.
> 
> Otherwise you will for sure forget the one or the other way that somebody
> wants to extend (read: inherit) your class.
> 
> I have been annoyed from many classes that use most things private
> for non obvious reasons.
> 
> > protected allows anyone to use the method by deriving from the base
> >class.
> 
> Yes, that's what it is for.
> You can either write end-user classes that
> virtually nobody can inherit because the interface is of not much use,
> or you trust in people (and your programming skills) and open things
> offering protected to most internals.

 The idea of a class is to define the interface and hide the
implementation.
I would argue that if a base class needs fixing up before it is usable
then
something is wrong. My main arguement against such practice is that it
becomes
difficult to understand the behaviour of the base as a seperate unit. I
speak
from experience, I had a design in which I had base class which allowed  
derived classes to change its state via protected function calls. I
thought
that I could trust myself but it all went sadly wrong, I could no longer
understand the state transitions of the base class and ended up
rewriting.

 There has been much discusion in comp.object about the merits of
inheritance as a means for extending a class. "Design patterns" by the
gang
of 4 avocate the use of composition rather than inheritance for the
extension
of functionality.  So part of my argument stems from an oppostion
to the use of inheritance. Having said this Qt makes extensive use of
inheritance
with the user Widgets implimenting the interface by overriding virtual
functions.
(I will need to think again if this discussion was about virtual
functions)

 However I would still argue it is good style to make *nonvirtual*
functions either
private (part of the internal workings) or public (part of the
interface), protected
seems to sit on the fence and it certainly confuses me.
I would also strive to keep all the data private. I will however conceed
that 
these are only my view points.


 cheers Paul.



 cheers Paul.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic