Java の保護されたキーワード

WBOY
リリース: 2024-08-30 15:22:35
オリジナル
235 人が閲覧しました

保護されたキーワードは、変数、メソッド、コンストラクターにアクセスできる範囲を制限するために使用されるキーワードです。これは、Java のアクセス修飾子のタイプの 1 つです。これらは、メソッド、変数、コンストラクター、クラスのスコープを区別するために使用されます。 Java には 4 種類のアクセス修飾子があります。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト
  1. デフォルトのキーワード: パッケージ内でのみアクセスでき、パッケージ外から呼び出すことはできません。名前が示すように、アクセス指定子が指定されていない場合、デフォルトとして自動的に割り当てられます。
  2. 公開キーワード: プログラム内のどこからでもアクセスできます。これは、同じまたは異なるクラス、および同じまたは異なるパッケージから使用できることを意味します。
  3. プライベート キーワード: クラス自体の外部からのアクセスを許可しないことで、キーワードをより高いレベルに制限します。
  4. 保護されたキーワード: この記事では、保護されたキーワードについて詳しく説明します。

変数またはメソッドが保護済みとしてマークされると、以下のメソッドによってのみアクセスできます:

  • それが宣言されているのと同じクラス内。
  • 宣言されたクラスと同じパッケージ内にある他のクラスから。
  • クラスは、パッケージに関係なく、宣言されたクラスから継承されます。

保護されたキーワードは、クラス外の変数にアクセスし (プライベート キーワードの場合は不可能です)、特定のメソッドのみが同じものを継承できるように維持するために導入されたため、パブリック キーワードとプライベート キーワードの両方を組み合わせたようなものです。

構文

保護されたキーワードは、キーワードの前に「protected」というキーワードを付けて宣言されます。まず、以下のように「MyClass」というクラスの 1 つで protected キーワードを宣言します。

class MyClass {
protected String name = "Katy";
protected int token= 55;
}
public class SubClass extends MyClass {
public static void main(String[] args) {
SubClass obj = new SubClass();
System.out.println(obj.name + "'s token number is: " + obj.token);
}
}
ログイン後にコピー

ここでは、クラス「SubClass」は「MyClass」を拡張しているため、SubClass のオブジェクトを作成し、変数を呼び出すことで、保護されたキーワードをここで使用できます。

出力:

Java の保護されたキーワード

保護されたキーワードはメンバー レベルでのみ使用できます。つまり、関数の外で宣言された静的でない内部クラスです。 protected キーワードは、クラスの外部および別のパッケージのサブクラス内でアクセスできるため、private のキーワードとは異なります。

保護されたキーワードの使用に関する制限の一部は次のとおりです:

  • クラスを保護されたものとして宣言するために使用することはできません。
  • インターフェイスは保護されていると宣言できません。
  • パッケージ外部からのアクセスは継承を通じてのみ可能です。
  • 保護されたコンストラクターは、インスタンスを作成してパッケージの外部にアクセスすることはできません。

Java での保護されたキーワードの例

保護されたキーワードの概念をよりよく理解できる例をいくつか見てみましょう。

1.親クラスを拡張せずに protected キーワードを呼び出す

ここでは、「package1」の親クラスからキーワードを呼び出してみます。 「package2」には「ProtectedExample2」が作成されており、ここではキーワード「disp」を呼び出しています。ただし、子クラスはメイン クラスから値を継承していないため、コードはキーワードにアクセスできず、示されているように例外をスローします。

コード:

package com.package1;
public class Example {
protected String disp="Printing message from protected variable from package1";
}
//Create new package as com.package2
//Create new class as ProtectedExample2
package com.package2;
import com.package1.Example;
public class ProtectedExample2 {
public static void main(String[] args) {
ProtectedExample2 a=new ProtectedExample2();
System.out.println(a.disp);
}
}
ログイン後にコピー

出力:

Java の保護されたキーワード

2.保護されたクラスへのアクセス

この例では、保護されているクラス「ProtectedExample5」にアクセスしようとします。これによりコンパイル エラーが発生します。

コード:

protected class ProtectedExample5 {
void display()
{
System.out.println("Try to access outer protected class");
}
public static void main(String[] args) {
ProtectedExample5 p=new ProtectedExample5();
p.display();
}
}
ログイン後にコピー

出力:

Java の保護されたキーワード

3.同じパッケージで異なるクラスの保護されたキーワードを表示します

以下の例では、まず「com.package1」というパッケージを作成し、「Example」という名前で新しいクラスを作成します。ここで、キーワード「disp」が保護されていることを宣言します。この保護されたキーワードをクラス「Example1」を使用して表示してみます。このためには、まず親クラス「Example1」のオブジェクトを作成し、キーワード「disp」に割り当てられた値を出力する必要があります。

コード:

package com.package1;
public class Example {
protected String disp="Printing message from protected variable from package1";
}
class Example1 {
public static void main(String[] args) {
Example obj=new Example();
System.out.println(obj.disp);
}
}
ログイン後にコピー

出力:

Java の保護されたキーワード

4. Displaying protected keyword from a different package

Using the same code as above, we shall see how to call the protected keyword by creating a different package, “package2”. A protected keyword can be accessed only through inheritance from package1; hence “ProtectedExample2” is extended from “Example”. In a similar way as the first example, we have to create an object of the class “ProtectedExample2” in order to access the protected keyword from package “com.package1”.

Code:

package com.package2;
import com.package1.Example;
public class ProtectedExample2 extends Example{
public static void main(String[] args) {
ProtectedExample2 a=new ProtectedExample2();
System.out.println(a.disp);
}
}
ログイン後にコピー

Output:

Java の保護されたキーワード

5. Accessing a protected class by overriding to sub-class

Here the class is declared as protected inside the inherited class “Example5”. Also, a protected class called “Example” is declared outside the function but in the same package. When an object of “Example5” is created and the protected class “disp()” is called, we can observe that the overridden method is called instead of the outside class. This is because we shall not be able to import “com.package1” and its class “Example” since it is not visible and causes a compilation error.

Code:

//Create a file by Example.java
package com.package1;
class Example
{
protected void disp()
{
System.out.println("Printing from protected class in the outside function");
}
}
//Create a class by the name Example5.java
public class Example5 extends Example {
protected void disp()
{
System.out.println("Accessing the overriden function");
}
public static void main(String[] args) {
Example5 exp=new Example5();
exp.disp();
}
}
ログイン後にコピー

Output:

Java の保護されたキーワード

Importance of Protected Keyword

The importance of protected keyword in java is:

  • These keywords allow the classes or their variables to be inherited from their parent class which is not possible with any other restricted keyword such as private.
  • A protected keyword is the combination of a private keyword’s advantage and that of a public keyword. It eliminates the disadvantage of public keyword that the variable or class be accessible from anywhere in the program by restricting the scope.

Conclusion

As shown in the above examples, we choose protected keywords depending on the access level we require at the code level. They help greatly in cases where the same variable or class needs to be accessed from other inherited methods in the program. A parent-child relationship is always present between the parent class and its sub-classes which are using the protected keyword.

以上がJava の保護されたキーワードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!