welcome to nerest technologist I'm bangar Raju and in this video I want to demonstrate you about access specifiers access specifiers what are these aess specifiers see aess specifiers these are a special kind of modifiers what are that actually all the keywords whatever we just have are generally called as modifiers see so many keywords what are available for is a static public private uh virtual abstract override seal all all these are just going to be called as modifiers okay a special kind of modifier using which we can Define the scope of a type and its members it's a special kind of modifiers using which we can Define the scope of a type and its members so what is that scope scope in the sense accessibility or visibility simply speaking who can consume who cannot consume so I have a class with some setup members these members should be accessible to home should not be accessible to home all these things can be controlled by using the access specifiers and actually C supports five access specifi what are those first private second internal protected protected internal and lastly public so we have five different access specifiers available in C private internal protector protected internal and public and each and every modifier or each and every access specifier has a different scope so they're going to have a different scope what is the scope of each and everything let us practically understand about it okay let me open the visual studio a new project I'm opening a console application we'll call it as access demo one and also the solution name I'm changing as my solution if you don't change the solution Name by default it will be access demo one but I'm changing the solution name okay which comes with a default class called as program let me make this classes Public public actually we don't require for any discussion on public if you declare public the meaning is accessible from anywhere it is simply going to be accessible from anywhere so no restrictions from where you can just call it it can be called from anywhere you just want so that's what public is so public class program in this class I want to Define five methods to you and the five methods with five different Scopes are five different access specifiers first one a private method I'm naming it as private method second internal world test two internal method protected void test three protected method protected internal vo test for public method so totally I defined five different uh methods here with five different access specifiers now the important point to remember here is a member of a class that is defined with any scope is always accessible within the class if there are any restrictions the restrictions will start when you go outside the class so within the class no restrictions will be there so because within the class there will not be any restrictions you can call all the five methods right now from this class watch pram p is equals to new program P DOT test two test three for test five console. read line so calling all the five methods here just calling all the five methods here okay and you can just run and see all five methods are executed so no restrictions why every method is always accessible within the class when you just call it everything is accessible now if there are any restrictions coming into picture that will come when you go outside the class let us see what are the restrictions now watch right now your class is programmed with five methods I want to call these methods from another class to test that I'm adding another class in the project add a new class I'm naming this class as 2. CS class two colon program I'm inheriting so once I inherit according to the rules of inheritance parent class members will now become the child class members and child classes can access the parent class members as if it is the Warner of those members so now static void main create the instance of the class two and once after creating the instance of the class two t Dot and we can call the methods but notice you are able to call only four methods but not five methods why because the first method test one is a private method what is private private in the sense the method is access only within the class in which it was defined the method is accessible only within the class it was defined so right now we are out of the class child class child class cannot access that is what private is and don't forget this the default scope for every member of a class is private every member of a class is by default a private member okay and another important point we cannot declare types as private you cannot declare class as private so three important points to remember Point number one members declared as private under a class are accessible only within the class Point number two the default scope for the members of a class in C is private Point number three we cannot declare types as private so we cannot declare private class you cannot apply private on a class watch right now I can call all this particular methods here what I can call is only four methods okay we'll call it just a minute just a minute let me go to the program class back again and I want to write this class as private error you're getting error this is a different error elements defined in the namespace cannot be explicitly declared as private protected or protected internal three things we cannot use private protected protected internal these cannot be used on a class you cannot you cannot use this three on a class what you can use in a class internal public that's all now I made public if I don't make public it is internal if I don't make public it is internal what is the scope of internal we come across it but the point remember private classes are not there protected classes are not there protected internal classes are there are not there we have only internal classes and public classes right no public and no discussions on Public public anyone can access that is what public is well come back to the class two and right now I showed you how many methods can I call now I can call four methods T do test 3 T do test 4 and T do test five console dot rate line I can call these four methods now and private method is not accessible to you okay now to run the class open the solution Explorer right click on your project and go to its properties go to Startup object and set the startup object as class two and run your class only four methods okay so the scope of private is clear now what is that members declared as a private in a class are accessible only within the class the default scope for the members of a class in C is private and we cannot use private on a class no private classes fine so right now we are consuming members of the class program from class two what is class two child class this is a child class first one class program you're consumed from the same if you just understand what we are doing here now consuming members of a class from same class consuming members of a class from same class but if you come to class two what are you doing in class two consuming members of a class from Child class you're consuming from Child class when you're consuming from Child class private members are not accessible to you okay one more thing so private is clear we'll talk next the other way how you can consume the members of a class from another class is by creating the instance without inheritance right now consumption is done through inheritance without inheritance also we can consume the members of a class how by creating the instance watch it let us take one more class in the project I'll name this class as class three from class three I want to call the members of class program from class three I want to call the members of class program okay how to call without using inheritance I want to call how can you call without using inheritance yeah it is possible by creating the instance of the class how program p is equals to new program creating the instance and now I can call this how many methods can I call have a look 2 4 five one is not accessible already clear within the program only we can consume three is also not accessible what is three three is protected two is internal three is protected protected is also not accessible why protected is not accessible simple members declared as a protected in a class are always accessible able only within the child class not accessible from non-child class right now class 3 is non-child class class two is child class you can consume in class two but not in class three why child classes can consume non-child classes cannot consume and very importantly you cannot use protected on class also already we read there three things we cannot use in a class private protected protected internet these three cannot be used on a class okay you can use only on the members and if you're declare any member as protected accessible only to child class non-child class cannot access it watch I'm able to call to four and five console dot R line and uh what are we seeing here consuming members of a class from nonch class so three different scenarios what I showed you right now first one consuming members of a class from same class second consuming members of a class from Child class third consuming members of a class from non-child class and right now run this class sorry somewhere semicolon is missing oh here all the three methods fine so we are able to call so what we are not able to call now protected so right now I showed you three different cases what are the three different cases this is case one consuming members of a class from same class and this is case two consuming members of a class from Child class and this is case three consuming members of a class from non-child class okay and in this process we learn three Scopes now public no restrictions at all can be accessed from anywhere no rules second private can be accessed only within the class cannot be accessed outside of the class third protected can be access can be accessed within the class as well as within the child classes non-child classes cannot access two more things to learn okay to describe the remaining two uh I want to just Define two more classes what are the two more classes you know the fourth one is case four what is case four consuming members of a class from Child class present in a different project right now we have a child class in the same project non-child class in the same project but I'm going to show you two more cases child class in different project non-child class in different project what I'm trying to tell is class two consuming members of a class from Child class of same project class three consuming members of a class from non-child class of same project same way we can have a child class in a different project non-child class and different project also that is also possible we'll see that to do that first under the solution I am adding a new project under the solution I'm adding a new project okay right click on the solution select add new project and in the new project window select your language as visual shop and the project template as console application and let me call it as access demo 2 access demo 2 and this is under the my solution first one is access demo one this is access demo two click okay both projects are under the same solution now you can say one project and the second one is getting added two projects access demo one access demo two and here also by default I have a class called program but I want to rename this class as four two three we have there I want to rename this as class 4 right click rename four when you rename this automatically what is going to happen is the class also will be renamed It'll ask you shall I rename everything or not yeah you're renaming a file would you also like to perform a rename in all projects of the reference to the code element program it adding do you want to do it yes I'm renaming everything so right now the class name also gets renamed as four Now what is my intention you know my intention is to consume the members of the class program in class 4 the members of the class program in class 4 by inheriting I want to inherit now but these two classes are in two different projects because they're in two different projects what you required to do first you're required to add a reference of that assembly to this project so open solution Explorer right click on the project access demot to and you'll find an option called add reference click on it that opens a window called as reference manager okay adds you a reference called as reference manager okay just a second go to browse and select the assembly from its reference from its location where did I save it C drive a folder called as C my solution access demo one bin debug access demo 1.
this is assembly add the reference of this assembly once the reference is added you can start consuming that class now how to consume two ways of consumption inheritance object creation I want to inherit colon access demo one dot class name program inheriting so if you just understand this also a child class how is it different from class two Class 2 is also child class and class 4 is also child class the difference is class two is a child class of same project class 4 is a child class in different project so I'm just going to write command for class 4 also from Child class of different project child class of a different project come here I want to just go for consuming the members create the instance of class 4 4 f is equals to new 4 and now I want to call the members now so how many methods are accessible let us see F Dot three what are the three protected protected internal public and three things are clear for us private cannot be accessed public can be accessed from anywhere and next protected yeah protected also accessible from where from Child class currently class is child class class 4 is child class now I can access so our confus is only about internal protected internal and watch now internal is not accessible what is internal internal means if you declare a member or a class as internal it is accessible only within the project from a child class as well as non-child class also but non-child class or child class from anywhere you can consume but cannot be accessed out of the project so that is what internal is so right now test two test two is internal method you are able to call it in case 2 case three also but you cannot call it in case 4 why you cannot call because this is out of the project and that is accessible only within the project but not accessible outside the project that is what internal is and another important Point remember the default scope for a class is internal only that is why every class is always accessible within the project but not out of the project until it is declared public watch here when I said your access demo dot you are not able to see class two and class 3 here why Class 2 class 3 are not specified with any scope so default scope is internal so we are out of the project those two are not accessible but program is accessible why because it is public okay on a class you can apply internal that is default or public but remaining three options are not available but members can have all the five fine now you can just call F do test three F do test 4 and F do test five console dot rate Line open solution Explorer right click on your project access demo 2 and select the option set as startup project to make it as a startup project and run the project you able to call protected protected internal public and private not accessible internal not accessible so we have seen four different cases and in these four cases we understood four Scopes what are the four private internal protected public one and only one thing is not clear what is it protected internal nothing to speak on protect internal protected internal is a combination of protected and internal simply speaking if protected or internal any of these two are accessible then protected internal also will be accessible if these two are not accessible protected internal also will not be accessible that's what protected internal is watch it I will add a new class in the new project a class I'll call it class five now this class five I want to call the methods of program class in this class five I want to call the methods of the program class but not by inheriting by creating the instance static void main access demo 1. program p is equals to new access demo 1.