Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Advertisement

Powered by Blogger.

Visitors

Flag Counter

Pages

Learning Java OOP: Understanding Levels of Member and Class Access (Modifier)

Java Oop Modifier
Classes in Java programs can be interconnected by giving access to their members.
One of the class relationships we have studied is inheritance .
Everything in the class (attributes and methods ) is called a member. Usually there will be an access level called a modifier.
In an inheritance relationship , all members in the parent class will be accessible by the child class (subclass), unless the memeber is given a private modifier .
Keep in mind:
Modifiers can not only be given to members. But it can also be given to interfaces, enums, and classes themselves.
Consider the following program code:
Modifier in java
What I underline in the code above is the modifier. This modifier will later determine the member and class access limits.

There are 3 Kinds of Modifiers in Java

In general there are three kinds of modifier used in Java:publicprivate, and protected.
If we do not use these three keywords, the member or class does not use a modifier (no modifier) .
Each modifier will determine where members can be accessed.
The following is the range table for each modifier:
ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
modifier noYYNN
privateYNNN
Information:
  • Y meaning it can be accessed;
  • N meaning not accessible;
  • Subclass means child class;
  • World meaning all packages in the application.
In the table above ... if we don't use a modifier (no modifier), then the class and member will only be accessible from the Class itself and the package (the class that is in one package with it).
In order to be accessible from anywhere, we must provide a modifier public.
Let's look at an example ...

1. Public

Modifier publicwill make members and classes accessible from anywhere.
Example:
package modifier;

class Person {
    public String name;

    public changeName(String newName){
        this.name = newName;
    }
}
In the class Presonthere are two members, namely:
  1. attribute name
  2. method changeName()
We give both modifiers publicThis means that they will be accessible from anywhere.
However, Personwe don't give class modifiers. So what will happen is that the class cannot be imported (accessed) from outside the package.
The experiment accesses the class person from outside the package
The experiment accesses the class person from outside the package
The class Personis in the package modifier, then we try to access it from the default package , so what happens is an error like the picture above.
What is the solution so that it can be accessed from outside the package?
Yes we have to add modifiers publicto the class Person.
Add a modifier to the class person
Add a modifier to the class person
Then the error will disappear and the class Personwill be imported from any package.
In the class diagram, the modifier is publicrepresented by a plus ( +symbol .
Example:
Class diagram with public members
All members in the class Playerhave modifiers publicNote the symbol +in front of it.

2. Private

Modifier privatewill make members only accessible from within the class itself.
Keep in mind:
Modifiers privatecannot be given to classes, enums, and interfaces. Modifiers privatecan only be given to member classes.
Example:
class Person {
    private String name;

    public void setName(name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }
}
In the example above, we give a modifier privateto the attributes nameand modifiers publicin the method setName()and getName().
If we try to access the attribute directly name like this:
Person mPerson = new Person()
mPerson.name = "Petani Kode"; // <- maka akan terjadi error di sini
Do not believe?
Try it in Netbeans:
Error when accessing private members
Then, how do you access members privatefrom outside the class?
We can use setter and getter methods . Because, this method will always be given a modifier public.
Example:
Person mPerson = new Person();
mPerson.setName("Petani Kode");

System.out.println("Person Name: " + mPerson.getName());
In the class diagram, the modifier is privaterepresented by a minus ( -symbol .
Private Member Class Diagram

3. Protected

Modifier protectedwill make members and classes only accessible from:
  1. Class itself;
  2. Sub class or child class;
  3. Package (class in one package with it).
Modifiers protectedcan only be used on members only.
Example:
package modifier;

public class Person {
    protected String name;
    
    public void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }
}
In the example above, we give a modifier protected to the attribute name.
If we try to access from a class that is one package with it, there will be no error.
However, if we try to access from outside the package like this:
import modifier.Person;

public class Author {
    
    Person p = new Person();

    public Author() {
        // akan terjadi error di sini karena atribut name 
        // telah diberikan modifier protected
        p.name = "Petani Kode";
    }
    
}
Then an error will occur.
Error Modifier Protected
In the class diagram (in StarUML), the modifier is protectedrepresented by a fence ( #).
The modifier symbol is protected in the class diagram
Sumber: https://www.petanikode.com/java-oop-modifier/
0 Komentar untuk "Learning Java OOP: Understanding Levels of Member and Class Access (Modifier)"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top