受保護的關鍵字是用來限制變數、方法和建構函式的存取範圍的關鍵字。它是Java中存取修飾符的一種類型。它們用於區分方法、變數、建構函數和類別的範圍。 Java中有4種類型的存取修飾符,它們是:
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗一旦變數或方法被標記為受保護,就只能透過以下方法存取:
受保護的關鍵字就像是公用和私人關鍵字的組合,因為引入它們是為了存取類別外部的變數(這在私人關鍵字的情況下是不可能的),並維護只有某些方法可以繼承相同的變數。
受保護的關鍵字是用其前綴為「protected」的關鍵字來聲明的。我們首先在名為「MyClass」的類別中宣告 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 的物件並呼叫變數來使用 protected 關鍵字。
輸出:
受保護的關鍵字只能在成員層級使用,即在函數外部宣告的非靜態內部類別。 protected 關鍵字與 private 不同,因為它們可以在類別外部和另一個套件的子類別中存取。
使用受保護關鍵字的一些限制是:
讓我們透過一些範例來更好地理解受保護關鍵字的概念。
這裡我們嘗試從「package1」的父類別中呼叫關鍵字。 「ProtectedExample2」是在「package2」中建立的,這裡呼叫了關鍵字「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); } }
輸出:
在此範例中,我們嘗試存取受保護的類別「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(); } }
輸出:
在下面的範例中,我們先建立一個名為「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); } }
輸出:
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:
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:
The importance of protected keyword in java is:
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中文網其他相關文章!