protected vs. private convention

camelCase

The Case of the Mysterious Camel.
Reaction score
362
When I want to restrict access to certain class members, I use protected members by default.
This is so I can derive from that class easily without worrying about being unable to use methods/variables from the base class.

I hardly (if ever) use the 'private' access modifier and only use it if I'm sure I only need to make a method/variable accessible to that class and that class alone.

I thought that everyone did what I do until today's programming class at school today.
The lecturer was saying that you should do the reverse.

Make all members/methods that you want to control private and make it protected only when you want to work with inheritance.
This got me a lil' confused so I'm here asking for opinions =x

It probably doesn't matter and is probably up to the programmer but which convention is more widely used and why?
And is one convention really better than the other?

[EDIT]
I might as well toss in another question about singletons.
What's the difference between a singleton and a class with all members/methods static?
 
I agree with your lecturer. Making everything available to an inheriting class is pretty bad design.
 
I'm from the school that considers properties to be part of the object and not part of the class. In that school, declaring variables local to the class (but somehow still inside the object) doesn't make any sense what so ever. I'd use object composition if I wanted to preserve the invariants you typically try to preserve by using "private". In any case, a subclass can just override all public methods and change the observable behavior that way. Using private doesn't prevent that.
That said, if I can save half a day of refactoring by just writing private I usually take the easy route :) That doesn't happen very often though.


Edit: I guess that doesn't answer your question at all. A wild guess is that using private is way more common.
But I'd take almost the opposite stance of Vestras and conjecture that: If the distinction between "private" and "protected" matters in your code then it's likely to be badly designed.

What's the difference between a singleton and a class with all members/methods static?
In practice, nothing really.
There might be some differences in how your implementation handle them (for example how much memory is used before you initiate the singleton and what kind of memory is used). If your language doesn't have first class classes (lol) (Java or C# doesn't without using reflection) then you wont be able to "pass around" a static class but you can pass around a singleton.
 
A difference about a Singleton and a static class is that static classes are initialized at program start. You can't really control where.
Singletons can be initialized when they're called first. That depends on the implementation of the Singleton though.
It also nicely allows polymorphism since you can store whatever class derives from the one your singleton handles.


I always make all of my member variables private, until I consciously decide to turn them into public/protected.
One idea of OOP is making easy interfaces and to encapsulate data. That means you try to hide all internal mechanics from external eyes.
 
Making everything available to an inheriting class is pretty bad design.
Oh..
Guess I get funny ideas here and there =/

I'm from the school that considers properties to be part of the object and not part of the class. In that school, declaring variables local to the class (but somehow still inside the object) doesn't make any sense what so ever. I'd use object composition if I wanted to preserve the invariants you typically try to preserve by using "private". In any case, a subclass can just override all public methods and change the observable behavior that way. Using private doesn't prevent that.
That said, if I can save half a day of refactoring by just writing private I usually take the easy route That doesn't happen very often though.
I read that and my head exploded =x
It seems like it might be interesting but my head can't parse it at this moment =/

If the distinction between "private" and "protected" matters in your code then it's likely to be badly designed.
That statement seems like it could spark a debate =x
But I've never found a real need for both private and protected at the same time (even though I know the difference between the two).
In fact, I don't think I've *ever* used the 'private' access modifier except in test programs =/

A difference about a Singleton and a static class is that static classes are initialized at program start. You can't really control where.
Singletons can be initialized when they're called first. That depends on the implementation of the Singleton though.
It also nicely allows polymorphism since you can store whatever class derives from the one your singleton handles.
Those are really good points ;O
I now view Singletons as totally separate from static classes ._.

I always make all of my member variables private, until I consciously decide to turn them into public/protected.
One idea of OOP is making easy interfaces and to encapsulate data. That means you try to hide all internal mechanics from external eyes.
Huh..
I always felt that 'protected' provided all the encapsulation I needed.
Guess I need to program moar =/

Okay, so..
A class would normally have *only* private members.
A class will have protected and/or private members if it serves as a base-class.
Of course, base-class or not, both will have public members.

Right.. ?
 
A class would normally have *only* private members.
A class will have protected and/or private members if it serves as a base-class.
Of course, base-class or not, both will have public members.

Well, I only make everything private because I didn't know about protected when I started to program. So it became a habit xD

But basically I only change the private access when there's a good reason for it.
Either I need to because of inheritance.
Or I think it's ok to allow the outside to use this function/variable.

For example
Code:
class Point{
public:
    int x;
    int y;
}
I'd be stupid to make getters/setters for those -_- xD


If the distinction between "private" and "protected" matters in your code then it's likely to be badly designed.
Especially in libraries it's important to differentiate between private and protected.
If you create a superclass from which users can inherit their own classes it's important to keep critical internals away from them, while you might still want them to have access to other stuff.
One could argue that everything else could be declared public, since the user has access to it through the inherited subclass anyway. But that would completely defeat the idea of data encapsulation.
 
Thanks, S3rius!
Seems like that pretty much convinced me to use private over protected =x
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top