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?
 

Vestras

Retired
Reaction score
249
I agree with your lecturer. Making everything available to an inheriting class is pretty bad design.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
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.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
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.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
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.. ?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
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.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top