0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java】アクセス修飾子を理解してみた

Last updated at Posted at 2025-04-26

アクセス修飾子を理解してみた

Javaのアクセス修飾子について
探せばどこにでもあるけれど、個人的に理解した内容でメモ

きっとこのようになる

  • 同一パッケージ内:

    • 同一クラス:
      • アクセス修飾子にかかわらずアクセス可能
    • 異なるクラス
      • Public,Protected,Defaultにはアクセス可能
      • Privateにはアクセス不可
  • 異なるパッケージ:

    • Publicにはアクセス可能
    • Protectedはアクセス不可
      • 例外的に子クラスの場合アクセス可能
    • Default,Privateはアクセス不可

多分図にするとこんな感じ

image.png

間違ってたらおしえてください。

練習:ソースコード

Example.java
package samePackage;

public class Example {
    public int publicField = 1;
    protected int protectedField = 2;
    int defaultField = 3; // Default access
    private int privateField = 4;

    public void showFields() {
        System.out.println("Public: " + publicField);
        System.out.println("Protected: " + protectedField);
        System.out.println("Default: " + defaultField);
        System.out.println("Private: " + privateField);
    }
}

// 同じパッケージ内でアクセス

AccessWithinSamePackage.java
package samePackage;

public class AccessWithinSamePackage {
    public static void main(String[] args) {
        Example example = new Example();
        System.out.println("Accessing fields within the same package:");
        System.out.println("Public: " + example.publicField); // OK
        System.out.println("Protected: " + example.protectedField); // OK
        System.out.println("Default: " + example.defaultField); // OK
        // System.out.println("Private: " + example.privateField); // Compile error
    }
}

// 別パッケージからアクセス

AccessFromDifferentPackage.java
package differentPackage;

import samePackage.Example;

public class AccessFromDifferentPackage {
    public static void main(String[] args) {
        Example example = new Example();
        System.out.println("Accessing fields from a different package:");
        System.out.println("Public: " + example.publicField); // OK
        // System.out.println("Protected: " + example.protectedField); // Compile error
        // System.out.println("Default: " + example.defaultField); // Compile error
        // System.out.println("Private: " + example.privateField); // Compile error
    }
}

// 継承を使った例

SubClass.java
package differentPackage;

import samePackage.Example;

public class SubClass extends Example {
    public void showProtectedField() {
        System.out.println("Accessing protected field from subclass:");
        System.out.println("Protected: " + protectedField); // OK
    }
}
AccessProtectedFromSubclass.java
package differentPackage;

import samePackage.Example;
public class AccessProtectedFromSubclass {
    public static void main(String[] args) {
        SubClass subclass = new SubClass();
        subclass.showProtectedField();
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?