Access Modifiers

From our previous post on Modifiers, we got to know that there are two types of modifiers exist.

  1. Access Modifiers
  2. Non access Modifiers
The first one deals with access related rules while the second one is purely behavioural.

In this post, we will get to know about the Access Modifier, what they can do and how to use them.

Let's start with a formal definition,

Access Modifiers: Access modifiers are special keywords which specify the accessibility of a Class, Constructor, variables and methods

What do you mean by accessibility ?
Hmm, let's take a scenario. You are a person, you have a family whom you stay with and you have a social profile which encompasses some of your best buddies, your neighbours, your colleagues etc.

Let's assume that I am living with my family where other member is Suman. Let's create representation of this family. We'll be going to create 3 classes. Let's see how they look like.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Aug 25, 2015
 */
public class Palash {
 public String name;
 static int age;
 private static boolean walkMood;
 private boolean tookShower;
 private boolean tookFood;

 /**
  * @return the name
  */
 public String getName() {
  return name;
 }

 public void reachOffice() {
  if (!tookShower) {
   takeShower();
  }
  if (!tookFood) {
   takeFood();
  }
  walk();
 }

 public static void walk() {
  if (walkMood) {
   System.out.println("Palash is walking");
  } else {
   System.out.println("Denied to walk");
  }
 }

 void takeFood() {
  tookFood = true;
 }

 private void takeShower() {
  tookShower = true;
 }

 public boolean isRichFamily() {
  if (FamilyDetails.familyIncome < 200000000) {
   return true;
  } else {
   return false;
  }
 }
}



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Aug 25, 2015
 */
public class FamilyDetails {
 protected static double familyIncome;
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Aug 25, 2015
 */
public class Suman {

 private static boolean walkMood;

 public int getPalashAge() {
  return Palash.age;
 }

 public boolean isRichFamily() {
  if (FamilyDetails.familyIncome < 500000000) {
   return true;
  } else {
   return false;
  }
 }

 public void walkWithPalash() {
  Palash.walk();
  walk();
 }

 public static void walk() {
  if (walkMood) {
   System.out.println("Suman is walking");
  } else {
   System.out.println("Suman denied to walk");
  }
 }
}

Hey...
Hold on. I will answer your questions. Take some time to read the following and you can find your answers in it.

OK, Notice things one by one,

Palash is having a variable name which is having public tagged to it. This public is an access modifier which defines that, this information is publicly available. Anyone can call it from anywhere.

Palash is having another variable age which is having a static tagged to it. Well, this one is not in the scope of this post. I will be reviewing this one in my other post. For now, just consider this as a keyword.

Palash is having variable walkMood which is tagged private. This marks that this variable is only available to the class and no one else can access its value. Well, Palash really will never allow anyone to play with his mood. So, this one perfectly deals with the visibility of Palash's mood and secures it to himself only. Same goes for tookShower, tookFood as well.

Then Palash has getAge(). Name is publicly available and anyone can access it. So, this method is also marked public.

Well, reaching office is also a publicly done activity. But check again, this is calling two other methods within it. These two methods are private.  Well, Palash will not allow others to access his shower or food !!!

So, only Palash can access these and use these as and when required.

What about walking with the other member in your family ?
Well, it depends on mood. So, walk is public. People can ask Palash to walk() but Palash only can decide if he will walk() or not depending on the private variable walkMood.

Anyone can ask Palash if he belongs to a rich family or not. He has set some rules himself when to call a family reach and answers the questions based on his rules. To get the family status, Palash accesses a variable from Class FamilyDetails. This variable is marked to be protected. This means that any member of the same package and any child class can access this variable.


No comments:

Post a Comment