Java Modifiers Types Definition

Java Modifiers Types

So, folks.. first of all what goes through your mind when you hear “modify”, well for me when I hear that word the simplest meaning is to “change”. So what is a modifier, basically modifiers (In java) are a keywords that you can add to those definitions to change their meanings. In the java language there are a wide range of variety for java modifiers types, including the following modifiers :

  • Java Access Modifiers¬†
  • Java Non Access Modifiers

Continue reading Java Modifiers Types Definition

Inner Class

How are you today folks? fine right? well i’m fine as well. Last time we just know that there are an un named class called Anonymous inner class, inner came because it is a subclass of a parent class, last time we get the example for anonymous inner class in an interface, well today we’ll talk about Inner clas. I should’ve create this before the anonymous inner class, but don’t mind it, everyone. And today i’ll redeem it.

Okay… can you guess what does inner class means? it has something to do with inner and a class, so.. have you thought about it? I might think it like this, hmm inner means it inside and class means class, so it must’ve mean inside a class. If you think it just like the way I think it, then congrats! you’re right, an inner class is a class that is inside another class, an inner class also can be called a nested class. Why does it says nested, it’s because the class that’s created is inside another class so you can call it nested. Here are a little example of how an inner class might look like :


class example{
  class nested_example{

  } 
}

You see that nested_example class is inside the example class, hence the name inner class. An inner class or a nested class can be divided into 2 types :

  • Non-Static nested class
  • Static nested class

Non-Static nested class :
An inner class works like a security mechanism in Java. Usually a class can not be associated with the access modifier of private. But using inner class, we can make the inner class to be private and after you set the inner class to be private then no object can accessed it from outside the classs.
There are 3 types of Non-Static inner class, and one of them is anonymous inner class, look at this :

  • Inner class
  • Method-local Inner class
  • Anonymous Inner class 

Creating a non-static inner class is simple, all you need to do is create a class inside another class and if the inner class is private then we use the method by first calling the public methods inside the same class as the inner classes are then use the method inside those other public inner class . If you don’t get that part easily, here are the real example :

class example {
 // the inner class
private class nested_example{
    public void print() {
    System.out.println(“Private inner class”);
  }
}

/* we’ll access the private class from the method within the same parent class which is inside
 example class
 */

void print_Inner() {
    nested_example nested = new nested_example();
    nested.print();
  } 

}

// we’ll create another class outside the example class 

public class outer_class {
    public static void main(String args[]) {
     // we’ll instantiate the outer_class
     example outer = new example();

    // we’ll access the print_inner method

    outer.print_Inner();
  }
}

And this will output “Private inner class”.

Static Inner Class :
A static inner class is a nested class that is a static member of the outer class. This means that we don’t have to instantiate the class and use the method from another static member. But, just like all static members, static inner class doesn’t have access to use the instance variables and method from the outer clas.
And this is how to use Static inner class :

public class example {

   static class nested_Example{
   public void static_Method() {
      System.out.println(“Static Inner Class”);
  }
}

  public static void main(String args[]) {
      example.nested_Example nested = new example.nested_Example();
      nested.static_Method();
  }
}

 And this will output “Static Inner Class”

Okay i’ll hope you understand this, and next time we’ll talk about Method-local Inner class.

Conclusions

  • An inner class is a class that is inside another class, an inner class also can be called a nested class.
  • There are 2 types of Inner class :
    • Non-Static Inner Class
    • Static Inner Class
  •  Thre are 3 types of Non-Static Inner Class :
    • Inner class
    • Method-local Inner class
    • Anonymous Inner class

Public, Private, Protected Access Modifiers

How are you today folks? Of course, when am I feeling not well? i’m fine as just always.We talked about the if and else statements last time, which is a statement that uses boolean expressions to decide whether a piece or a chunk of code inside the statement can run or not, it will use our logic to decide in wich certain f boolean expressions is right for a certain conditions. Well today we’ll talk about access modifiers, which consist of public, private, protected access, maybe you already seen a lot of public access in code that I usually write, well today we’ll dive right into it.

Right… now can you tell me what is the meaning of access modifiers in coding? can you or can not? probably can not for some of you, which i’ll tell you right now, 
access modifiers is a modifiers statement that will  modify and determine whether another class can acces its piece of code inside the class that the access modifiers is set in. It means that if you make 1 class and you set the access modifiers to private then the code you wrote inside can only be used or called inside the same class that you make, it can not be accessed throught another class outside.

Now, let’s start with the public access modifiers, can you tell me what is the meaning of acces modifiers? okay i’ll give you time to think… go! (start thinking, you can proceed after you think) come on guys I already  told you the meaning of access modifiers develop it. Okay i’ll assume that all of you have think at least a bit of what’s the meaing of public access modifiers, here let me tell you, a public access modifiers is an access modifiers that has the acces levels free to use and visible to the world, this means that the access level is very low. 

example of its code in real life :

public static void main(String[] args){
//…..
}

the main method of an applications needs to be public or else it can not be called by a Java interpreter to run the code inside the class.

Okay… the second one is protected access modifiers, which is the second most hight level access of a class which the code that writed can only be seen and called from the subclass or child class from the superclass or the parent class, this means that the code that is writed can not be seen or called from another superclass, for example if you create one superclass called apple, then you create another superclass called orange, with the protected access modifiers if you want to call the code that is written in apple then you need to call it within the apple superclass itself and the orange superclass can not see or call the code inside apple superclass, and so either way.

here are the example of its appliances in real code :

class VideoPlayer {
protected boolean openPlayer(Speaker sp) {
// implementation details
}
}

class StreamingVideoPlayer {
boolean openPlayer(Speaker sp) {
// implementation details
}
}

look at the boolean openPlayer, it started with the word protected and that is how you apply it in your code.

Pheww… i’m getting a little bit tired, but that’s not a problem, we need to finish this in the name of Coding!. Last one is the private access modifiers, which is the access level that has the highes level of access in modifiers hierarchy from strongest to weakest, in private access modifiers the code that is written in a class can only be called used inside that class itself, even another class from the same superclass cannot  call it, for example if you create a superclass apple then you create a subclass fruitMeat and write a code that is modify its access to be private, then you create another subclass fruitLeaf without any private access modifiers, then one time you want to call the code inside fruitMeat from fruitLeaf, and it will not work even when the fruitLeaf class is inside the superclass apple, because the code that is written in fruitMeat can only be accessed or called from inside the fruitMeat class itself, only the fruitMeat class itself!.

here are the appliances in real code, imagine that these 2 class are from the same superclass:

public class Logger {
private String format;
public String getFormat() {
return this.format;
}
public void setFormat(String format) {
this.format = format;
}
}
 
public class wood {
 
//any thing that doesn't call the String format 
 
} 

You see that in the wood class it can not call the String format from the logger class.

Conclusions

  •  An access modifiers is a modifiers statement that will  modify and determine whether another class can access its piece of code inside the class that the access modifiers is set in.
  •  A public access modifiers is an access modifiers that has the acces levels free to use and visible to the world, this means that the access level is very low. 
  •  A protected access modifiers is a modifiers that the code that writed can only be seen and called from the subclass or child class from the superclass or the parent class, this means that the code that is writed can not be seen or called from another superclass.
  •   In a private access modifiers the code that is written in a class can only be called used inside that class itself, even another class from the same superclass cannot  call it.