Saltar al reproductorSaltar al contenido principal
  • hace 5 semanas
En este video se explica el modificador de acceso default en java

Categoría

📚
Aprendizaje
Transcripción
00:00Hello everyone, in this video we are going to talk about this FAO access modifier or package modifier.
00:08This FAO access modifier has an access level that applies to members of the class
00:16when no explicit access modifier is specified
00:23That means that when we create a variable we are going to create a variable down here.
00:30an integer variable called A
00:34But we assign an access modifier to that variable. In this case, I want it to be public for the other classes.
00:44then I write here PULL
00:45and here we are defining a variable of integer type and that is a public access modifier
00:56But what is the package or FAO access modifier?
01:02is one that does not have an access modifier explicitly specified
01:09In this case we have an example with this class called products
01:17In this product class we have three attributes
01:21one of type string, one of type double and one boolean
01:29with its respective parameterized constructor to initialize the objects
01:35initialize the, to initialize the attributes of this class
01:42and a method that also has FAO access modifier
01:52That is, it does not have an explicit access modifier if it is public, if it is private or if it is protected.
02:00Well then as you can see the attributes and methods up to the constructor
02:10are found with FAO access modifier, package
02:17What are the advantages and disadvantages of having this package modifier or FAO?
02:27Well, one of the first advantages is that it can be accessed from the same class.
02:34from the same package
02:36but the disadvantage is that it cannot be accessed from a subclass
02:42not even outside the package
02:45What does this mean?
02:47that I can access these attributes and these methods
02:53being in that same package where the class is
02:57So if I believe myself
03:00I have a package called company
03:06Within that package is the products class
03:10and another class called test
03:13where the main method is
03:15So here we can see that in product
03:21We create a product object called corn
03:25we create your instance
03:27We initialize it as the product name corn
03:32its price and its stock
03:34but just as we can initialize it
03:38we can access it directly
03:41since we are in the same package
03:44where that kind of product is located
03:47and we can access it through class E
03:56and we can display the information that we assigned here by accessing those variables
04:07As you can see it shows us
04:10the values we had assigned to it here directly
04:15but if we comment on this line
04:18What it will show us are the values that we passed to the constructor
04:24so if we comment on them
04:27once commented
04:32we go back and execute
04:34will not show us this anymore
04:36but this
04:37and here it is
04:39if we go to another class
04:43outside the package
04:46we try to create
04:48an instance
04:49of the class
04:50of the product class
04:52As you can see
04:56when trying to access
04:58to class E
05:00when trying to create an object
05:02product
05:04It marks an error for us
05:06and why it marks us as an error
05:08because
05:09the builder
05:10has access
05:12from FAO
05:13which means that
05:16can only be accessed
05:18from their own class
05:20and within the same package
05:22but outside the package
05:24cannot be accessed
05:26and within the same package
05:28of the class
05:29and inside the package
05:31about the class
05:32because
05:32can be accessed
05:33from their own class
Sé la primera persona en añadir un comentario
Añade tu comentario

Recomendada