Jump to content
  • Advertisement
Sign in to follow this  
dr_slash_uh

private protected public...

This topic is 5060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok. I have been programming for years and there is one major thing that has been bothering me. And it is declaring data in my objects as private or protected. Why is this necessary? I understand get and set methods should be the only way to access data. Why not just declare them public and access them directly? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
It's not as much a problem if you code everything public for personal use. But say you're making an API that's to be used by anyone and everyone. They would access these public objects directly and perhaps change data or re assign pointers in such a way that it causes errors in your program.

Share this post


Link to post
Share on other sites
Thanks alot for the information. It sorta like coding for long term use.

Also, is polymorphism worth using? I am creating a stack that can have different objects. It seems to be a pain writing a function for a subclass, than a virtual function for the base class.

Share this post


Link to post
Share on other sites
The reasoning behind public and private takes a long time to understand. In fact, I'm not sure that I actually *understand* it now as much as I just accept it. The poster before me has the right idea, though. You don't always want people touching your private parts (teehee) if you're writing an API.

Two other lesser reasons for having it this way is that it allows for the creation of Read-Only variables (a member with a getter but no setter) and it allows you in some cases to bridge gaps in how the variable is actually stored versus what your code needs to know (for example, a GetLength() method of a CString class returns the length of the string.... but 'length' isn't actually a member of the class, but is generated on the fly each time the function is called).

Again, though, sometimes the reason isn't so clear when access levels interfere with your code, but it is useful when designing more complicated code.

Share this post


Link to post
Share on other sites
As for polymorphism, it is only useful in certain circumstances. I believe that polymorphism was invented solely to facilitate the development of GUI libraries (and it is EXTREMELY useful to that end). It allows the library developer to create large trees of classes that are all based on essentially the same code.

Share this post


Link to post
Share on other sites
Public variables can be accessed directly and are present in any classes derived from it.
Protected variables can only be accessed by methods of the class and are present in any classes derived from it.
Private variables can only be accessed by methods of the class and are not present in any classes derived from it.

The point of public, protected and private is to ensure data integrity. Classes make certain assumptions about the values of their member variables and by allowing public access to those variables, you remove all data integrity.

Take for example this:

void SomeClass :: SetValue(int iNewNumber)
{
if(iNewNumber < 100)
m_iMemberNumber = iNewNumber;
}






Now if m_iMemberNumber was declared as public, you'd have to add that if check every single place you want to set the value (through the public variable). Chances are, you'll forget the second or third time, and your data no longer has any integrity.

There's really no reason to declare your member variables as public, regardless of if you're working on an API or not. You will mess up your own software if you don't protect your data.

As for polymorphism, it's impossible NOT to see the value of it, after you've used it. There's no reason to copy the same data from class to class, when it can all be contained in a base class. Every enemy is going to have HP, and every soldier is going to have armour. So create an Enemy class with an HP member, then derive a Soldier from the Enemy class and add an armour value, and then derive RocketSoldier, SuperSoldier and Medic from that Soldier class.

Share this post


Link to post
Share on other sites

class EncapsulationWithoutInformationHiding {
private ArrayList widths = new ArrayList();
public ArrayList getWidths(){
return widths;
}
}


class EncapsulationAndInformationHiding{
private ArrayList widths = new ArrayList();
public List getWidths(){
return widths;
}
}

there must be a difference between ArrayList and List. so what is it?
it looks like the same code to me which does the same thing.

edit: i think i have an idea, but would still like to see what others thought.
from what i can the informationhiding is the fact that ArrayList is created then returned as a generic List. meaning that the user cannot know what kind of list he's dealing with. so does that mean that information hiding is only done through some sort of inheritance or composition (or just inheritance?) ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
there must be a difference between ArrayList and List. so what is it?
it looks like the same code to me which does the same thing.

edit: i think i have an idea, but would still like to see what others thought.
from what i can the informationhiding is the fact that ArrayList is created then returned as a generic List. meaning that the user cannot know what kind of list he's dealing with. so does that mean that information hiding is only done through some sort of inheritance or composition (or just inheritance?) ?


The basic idea here, as far as I can tell, is that with information hiding you can change the implimentation without affecting other peoples code. In other words, if in the future you decide that ArrayList is too inefficient and instead use a LinkedList, any code that is dependent upon your code won't break. In the former situation where ArrayList is returned, if you change the implimentation, you have potentially broken code because it is not unreasonable to expect that somewhere is a line like:

ArrayList foo = bar.getWidths();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!