From the course: Learning C#

Access modifiers - C# Tutorial

From the course: Learning C#

Access modifiers

- Let's take a closer look at how access modifiers work in C-sharp classes. These keywords let us control how the code and data within our class definitions are exposed to other parts of our program. Now, so far in this course, you might have noticed that some of the methods that we've been working with in our classes have the keyword "public" in their definitions. And you might be curious about what that means. The public keyword means that the method or class member can be accessed by any other code within your program. So there are a couple of other keywords we're going to try out in this example as well. "Protected" means that the method or class member can be accessed by other code within the class itself or within any derived subclasses. And "private" means that the method or class member can only be accessed within that class definition. And this is the default setting. Now, these are not the only access modifiers, but they're the ones that you're most likely to see in practice. And the others are a bit advanced for this course. So let's take a look at what this actually means in real code. Suppose I have class A, with these three method definitions and then within my main program I have some code that creates an object of class A. Well, the code to function A1 works because it's declared as public and that makes it available to any code in the program. However, the call to function A2 is going to be an error because it's declared as protected. So it's not available outside the class or A subclass. And then the call to function A3 is also going to be an error because it's private and therefore only available in class A. So now let's consider if I had class B, which derives from class A. Well, once again, the call to function A1's going to work because it's public. And now the call to function A2 also works because B is a subclass of A and therefore has access to that function. But the call to function A3 is still not allowed because only code that's inside class A's definition can use it. So let's go to our example in our editor and try this out. So here in VS Code in the modifiers folder, I'm going to open up the program and book files. And, what I'm going to do first, is make this name property public. And I want to emphasize here, by the way, this is not the right way to make a field accessible in your class. I'm just doing this for demonstration purposes. So bear with me. And then I'll make the other two fields. One of them will be protected and one will be private and it's default, but I want to be explicit here. So let's go ahead and save that. So now, if we go over to our program code... Now, remember, in a previous example, we couldn't modify this member directly, right? Because it was private by default, but this now works. So if I try this, let's go ahead and bring this up in our terminal and I'll dot net run. And now you can see that I'm able to change the title of the book. But again, this is not considered a good programming practice because, now I've created this tight coupling. So I've created this tight coupling between the program code here and the name of my field, which just isn't good programming practices. You want to make sure that there's a separation, so that you don't have this tight coupling between your objects and how they're used. So the other two fields are protected and private. So how can I make those accessible? Well, let me hide the terminal for a second. So one way to do this is I could just simply make public methods that operate on those fields, like this. So I could have a public string called "get name" and that would just return name. And then I could have public void set name, which takes a string and then just sets the name variable equal to S. And I can do the same thing for the other fields. So for example, I could have public void set author and that would set author to S. And then I could do the same thing with page count. So we'll call that said page count and that will take an integer. And then we'll set page count to the C. So now I have these intermediary functions that govern the access to my fields in here. So if I ever change these fields implementations the callers of these functions don't have to change anything. And then I can use those methods to change the book data. So let's save this and go back over to the program. And now I can operate on my book by doing something like this. I can call B1 dot set name, and that would be Grapes of Wrath. And then I can call the set author function. And then finally I can call site page count. And now we can just print out the description one more time. So now let's run the updated code and let's go ahead and comment these two lines out. So let's run this. You can see that it worked. So here we have the new title, the new author, and the new page count. So everything works now but it's a bit cumbersome to have to create individual methods to access each member data field. But if we're not supposed to make member fields public directly and writing individual functions for each field is too cumbersome. Then what is the right solution for allowing access to internal class data? The answer is to use a C-sharp feature called properties and we'll see how to do that in a separate video.

Contents