Access Modifiers and Packages

Complete Step By Step Java for Testers Inheritance, Access Modifiers And Abstraction
17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.20
List Price:  €130.49
You save:  €37.28
£79.91
List Price:  £111.88
You save:  £31.96
CA$136.56
List Price:  CA$191.19
You save:  CA$54.63
A$153.05
List Price:  A$214.27
You save:  A$61.22
S$136.08
List Price:  S$190.51
You save:  S$54.43
HK$782.75
List Price:  HK$1,095.88
You save:  HK$313.13
CHF 91.21
List Price:  CHF 127.70
You save:  CHF 36.49
NOK kr1,100.23
List Price:  NOK kr1,540.37
You save:  NOK kr440.13
DKK kr695.05
List Price:  DKK kr973.10
You save:  DKK kr278.04
NZ$168.07
List Price:  NZ$235.31
You save:  NZ$67.23
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,970.75
List Price:  ৳15,359.49
You save:  ৳4,388.74
₹8,335.10
List Price:  ₹11,669.48
You save:  ₹3,334.37
RM476.90
List Price:  RM667.68
You save:  RM190.78
₦130,608.93
List Price:  ₦182,857.73
You save:  ₦52,248.80
₨27,840.21
List Price:  ₨38,977.41
You save:  ₨11,137.19
฿3,694.31
List Price:  ฿5,172.18
You save:  ฿1,477.87
₺3,253.72
List Price:  ₺4,555.34
You save:  ₺1,301.61
B$515.88
List Price:  B$722.26
You save:  B$206.37
R1,886.05
List Price:  R2,640.54
You save:  R754.49
Лв182.13
List Price:  Лв254.99
You save:  Лв72.85
₩137,567.94
List Price:  ₩192,600.62
You save:  ₩55,032.68
₪380.78
List Price:  ₪533.11
You save:  ₪152.32
₱5,769.12
List Price:  ₱8,077
You save:  ₱2,307.87
¥15,680.08
List Price:  ¥21,952.74
You save:  ¥6,272.66
MX$1,724.22
List Price:  MX$2,413.98
You save:  MX$689.75
QR364.68
List Price:  QR510.56
You save:  QR145.88
P1,379.77
List Price:  P1,931.74
You save:  P551.96
KSh13,498.65
List Price:  KSh18,898.65
You save:  KSh5,400
E£4,789.68
List Price:  E£6,705.74
You save:  E£1,916.06
ብር5,693.24
List Price:  ብር7,970.76
You save:  ብር2,277.52
Kz83,560.30
List Price:  Kz116,987.77
You save:  Kz33,427.46
CLP$94,840.51
List Price:  CLP$132,780.51
You save:  CLP$37,940
CN¥724.59
List Price:  CN¥1,014.46
You save:  CN¥289.86
RD$5,873.12
List Price:  RD$8,222.61
You save:  RD$2,349.48
DA13,427.17
List Price:  DA18,798.58
You save:  DA5,371.40
FJ$225.97
List Price:  FJ$316.37
You save:  FJ$90.39
Q777.73
List Price:  Q1,088.86
You save:  Q311.12
GY$20,914.34
List Price:  GY$29,280.91
You save:  GY$8,366.57
ISK kr13,988.60
List Price:  ISK kr19,584.60
You save:  ISK kr5,596
DH1,011.63
List Price:  DH1,416.33
You save:  DH404.69
L1,776.81
List Price:  L2,487.61
You save:  L710.79
ден5,738.79
List Price:  ден8,034.54
You save:  ден2,295.74
MOP$805.67
List Price:  MOP$1,127.97
You save:  MOP$322.30
N$1,914.68
List Price:  N$2,680.63
You save:  N$765.95
C$3,678.96
List Price:  C$5,150.69
You save:  C$1,471.73
रु13,310.19
List Price:  रु18,634.81
You save:  रु5,324.61
S/372.97
List Price:  S/522.18
You save:  S/149.20
K385.04
List Price:  K539.08
You save:  K154.03
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,631.59
List Price:  ZK3,684.33
You save:  ZK1,052.74
L463.78
List Price:  L649.31
You save:  L185.53
Kč2,343.96
List Price:  Kč3,281.64
You save:  Kč937.68
Ft36,581.40
List Price:  Ft51,215.43
You save:  Ft14,634.02
SEK kr1,089.92
List Price:  SEK kr1,525.94
You save:  SEK kr436.01
ARS$87,365.86
List Price:  ARS$122,315.70
You save:  ARS$34,949.84
Bs692.26
List Price:  Bs969.20
You save:  Bs276.93
COP$394,057.74
List Price:  COP$551,696.60
You save:  COP$157,638.86
₡50,249.28
List Price:  ₡70,351
You save:  ₡20,101.72
L2,468.18
List Price:  L3,455.56
You save:  L987.37
₲742,598.03
List Price:  ₲1,039,666.95
You save:  ₲297,068.91
$U3,834.62
List Price:  $U5,368.62
You save:  $U1,534
zł402.81
List Price:  zł563.95
You save:  zł161.14
Already have an account? Log In

Transcript

Hello guys, welcome back to the series. Today we are going to discuss about packages and access modifiers. So let's first jump towards packages. What are packages? You guys might have noticed some of you might have noticed that all the programs we are creating our inside a default package that Eclipse creates for ourselves. What it does is package helps us in grouping of related types.

What I What do I mean by type here, types are different, like all the classes or interfaces all enumerations so it helps us grouping of related classes related interfaces in the same package. Let's say I'm working on so many projects and I need to know where my program files are with Where do where are they located in so many projects so i what i can do have is like different packages created for different sort of files. Maybe my cars related files are gonna be in one package and maybe in my automobile factory, I have one drugs manufacturing unit. So I'll have my trucks related files in one package, those kind of things to basically it helps us in managing and having a clear namespace. So what we can do is I can just create one more package to show you guys how we create package again, right click on the source, new packet, when maybe let him name me name it as auto, Bill.

And make sense. So from my last tutorial, let me just pull some of the classes to this guy gets cars and maybe BMW To my automobile package and eclipse will do the refactoring for us. So if you guys notice this inheritance, demo dot Java classes turn the default package. These two classes move to automobile package. So what Eclipse did is it added these two statements input automobile dot, BMW, an automobile dot cars. Automobile is the package name, BMW car system, class file name.

So that's what it is importing here to use the methods and variables from those classes in my inheritance, demo dot java class. So that's how a package is created. And that's how a package is used in some other namespace. So now, let's do one thing. Let's create one more class here to show you guys the access modifier Maybe domain name it has access Amani buyers demo main method an idea I'm gonna need here is maybe a car I'm just going to need the car subject that's it. So I don't even need that.

So what I'm doing here is I'm just again, hitting a reference to my car subject. I want to show something to you guys so just need to double check to them to reopen them all right. Because the open files were in different projects, so we need to reopen these files now see Have these credits to limit the need so how many types of access modifiers we have, we have private public protected and we have one last has no access modifier. I'm going to first show you guys what is a no access modifier, which is let's define this in speed. I haven't defined any modifier right. Maybe before jumping to this, let me explain you what modifiers So, you guys are pretty much familiar if you are noticing, every class we are creating, we are putting this public keyword every method we are creating we are putting this public keyword and I created this product keyword.

So, these keywords are access modifiers they limit the scope of these variables and methods where they can be visible and where they can be accessible. So basically, these are access modifiers so when I do not put any access modifier, what does it mean when there is no more Far, it means that particular variable or a method, or a constructor, basically, those are only available in the same package. It's mostly with the variables and the methods. And so my in speed, I can pretty much use it inside the same package. So let's call it her access modifiers demo. Let's see if I can use it see when that speed Yes, I can use it.

And maybe I can set it to 10. Yes, I will just set it to 10 successfully. So I can use this speed, which is defined with no access modifier in other class, which is inside the same package. Alright. Let's come to the next type, which is private. So let me define.

Let me call it the same thing speed and just change the name as private speed. So you guys would be able to easily sorry. And we'll go first and then need that type. Like that. So you guys know what the type of speed is just by the name. So let me now try it here.

See one.so? What happened? My private variable is not visible here, right? Yeah, that's what we expected, because I said, private variables, only class. Private variables. Private methods are only available in classes they cannot be used outside the class is one way if we really want to use it outside the class, we can use our setters and getter methods we can use ABC public wide and set private speed in the speed.

And maybe we can say IV speed, because to the speed. So when I'm setting it using my public method, so I just created a public method to set my private speed, I can actually use this, what I can do is, see when that I can use my method that I created, set private speed, and send maybe the limit as 10. So now I have just set my private variable with the help of public settlement. That's how We pretty much use the setter methods and getter methods to work with private instances private variables, private members, is one way. Let's move to other type. Maybe the public one, so let's call it from public and Lake speed and await.

Semicolon. And let's see if I'm able to use it. Hers he went down. Oh, yeah, public speed. Definitely. Yes.

So public speed is like, let's say yeah, maybe and then public since it is public, it is pretty much available anywhere. Even if I go to my other classes and my other packages, I just need to import them and it should be available. So let's open this class, which is importing my cars and BMW classes. I can pretty much use this even that and see probably Speed is available here. And if you guys notice my speed ones, just the speed without any modifier, it's not available here in my inheritance c c one dot speed is not available. So only public methods and public variables are available.

And if you see public speed is available set private speed, that method was public, this is available increased speed is available because that's public. That's it. That's a bit that's all this is available in any other class which is outside the packets right. So, no private variables, no variables without access modifiers are available outside the package. This is public. Maybe.

Let me define any There is Java with 14 class. Right? The next and the last type is productive. So if I define a productive variable protected and then speed what it can do? It's available where So guys, this variable are any method which is defined as protected. So I have a method here, decreased weight, which is defined as productive.

Both of these things should be available anywhere in any class within the same package. That's one thing which is similar to know accessing. One additional step that's available with the product type is there accessible to any sub class, even if the sub classes outside of packets. So let's see them let me just call them here. To see when that are protected speed is available let's say maybe, and using protected speed maybe as a speed limit. So it's available here.

Devices even that decrease speed is available here. So both of these are defined as productor. When this is selected, it's available here. If I go to inheritance towards our, let's see c one dot product ID speed is not available, all right? And my decreased speed is also not available. Okay, guys, yeah, increased speed is also not available because this class is not the Class of my past class.

And it's outside of the of the automobile packets. This is available to my BMW class, this would be, let's say, I call increased speed. So I can call producted speed definitely plus plus the cost. This is a subclass to my cost class. And also it resides in the same package with my cost us so I'm able to access this. Right?

I can also maybe create cars, you run across new cars, and I can also access my decreased speed because this is also declared as productive. And it's declared in automobile cars as a degree speedometer. So he will do accept that in BMW class because it's a supply To my caste class, and it exists in the same package. So guys, if you notice, I do not even need a reference to access the product ID speed variable, I can just use it directly in the subclass. See, I can use it right away in the subclass. That's the benefit of the product ID.

And but the thing is, it's only available in the same package and the subclasses. So since I was not able to access this in my inheritance demo, and which is in a different package, I want to show you guys the example of creating one more class, maybe, let's say on that class, and maybe we'll import our packages. So how do we import a package import auto, mobile dot star, or we can just say the car All only appears on the cars or we can say automobile that scar color to bring up everything within that one automobile pack. So this definer method here public wide increase on speed something different and what do I need to see here what's what all is available right? So let me create my instance cars. See one equals two new cars even that my public sphere is definitely available because it's public.

What's next my Said private speed is available. That's there. Let's maybe call it 10. And what next is available? Let's see. All right, sorry, we didn't make it as a subclass, we need to extend class in order to make it as a subclass.

And now let's see what else is available. Sorry. It's like my computer's getting slow. If I just do that my productive speed is available. You guys notice that? This is available because Condor classes are subclass to my cost class, even though it's in a different package.

I'm able to access my product, that speed variable. So that's what I was telling you guys. I think can pretty much access my productive speed variable anywhere in the same package or in a different packet if the class is a subclass to my car's class, because productive speed variable was defined in the cars class. So, this is pretty much that we want to discuss in seeing packets and sub classes. That is the definition of productive variables. So this is pretty much guys and I think this is quite a bit to absorb.

All you need to do is some practice to understand this to crab this. There are so many modifiers packages, just type some dummy code and try to access some variable some methods in different classes. And we see some issues just as all them work on them. Get your hands into the practice and you should be okay.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.