protected vs. private convention

camelCase

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

phyrex1an

Staff Member and irregular helper
Staff member
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.
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.
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.
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.
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:
    Friday Yay!
  • mgarcia mgarcia:
    did you guys catch Carl's interview? https://www.youtube.com/watch?v=kuHiMXABkGs
  • mgarcia mgarcia:
    he's the one that informed me about the DVD's working!
  • mgarcia mgarcia:
    he also mentioned the progress on the controllers!
  • The Helper The Helper:
    I did actually it was cool to see the NUON mention
  • The Helper The Helper:
    https://discord.com/channels/985377399338332202/985377399950696481 you can still use this chat too we are two fisted now :)
  • thewrongvine thewrongvine:
    costs me $80 to fill gas tank sad face
  • Ghan Ghan:
    Oof
  • The Helper The Helper:
    Yeah that gas is some expensive stuff :)
  • The Helper The Helper:
    ghan does not have to worry about it he has a tesla and I have a small tank so it does not cost me usually more than 50
  • The Helper The Helper:
    fyi Ghan we are getting an error trying to access stats or world editor tutorials Error 526 Ray ID: 72128c6bf99f6707 • 2022-06-26 02:35:15 UTCInvalid SSL certificate
  • Ghan Ghan:
    An artifact of switching to Cloudflare... the Let's Encrypt certs can't autorenew through Cloudflare.
  • Ghan Ghan:
    I got the forum updated before things expired but there were some other casualties.
  • Ghan Ghan:
    Everything should be fixed now....
  • The Helper The Helper:
    Thank you Ghan!
  • The Helper The Helper:
    Happy Monday!
  • The Helper The Helper:
    new NUON forum mod cubanral!
    +1
  • The Helper The Helper:
  • tom_mai78101 tom_mai78101:
    Started learning how to make tools-assisted speedruns, so I'm lately busy.
  • tom_mai78101 tom_mai78101:
    Here's my current project.
    +3
  • O Old Mountain Shadow:
    that was pretty good!
  • jonas jonas:
    really cool! I saw the game before but always thought it's just a half as good double dragon. Now I realize it actually has a lot of depth!
  • The Helper The Helper:
    I just saw a bunch of running and jumping past all the enemies look like it was scripted as he said I guess I should have watched the whole thing. I only got a couple of minutes in. You should post that video in the forum Tom
  • Darthfett Darthfett:
    "Hi in the chat!"
    +2
  • tom_mai78101 tom_mai78101:
    Currently busy with making the run even better, so I'll post that new one once I'm done. Right now, I had to modify the emulator itself, the tools that makes this video, and some RAM address disassembling / reverse-engineering to get the right values, and such.
    +1

    Staff online

    Members online

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top