Detailed explanation of Java basic access permission examples
1. Review of access modifiers
==public: The classes, properties, and methods modified by it can not only be accessed across classes, but also across packages
==private: Can modify data members, constructors, methods, but cannot modify classes. The members modified by it can only be accessed by the class itself and cannot be accessed by subclasses
==protected: Can modify data members, constructors, and methods, but cannot modify classes. It can be accessed by members of this class, the same package, or its subclasses (if it is a subclass, it can cross packages)
==Default: No Add any modifier to only allow access in the same package
2. Code example
##
package dog; public class Dog { // 公共的 public void publicMethod() { System.out.println("publicMethod"); } // 保护的 protected void protectedMethod() { // 被声明为protected的方法,可以被子类继承 System.out.println("protectedMethod"); } // 默认的 void defaultMethod() { System.out.println("defaultMethod"); } // 私有的 private void privateMethod() { // 这个方法只可以在类内部被调用 System.out.println("privateMethod"); } }
class T { public static void main(String[] args) { Dog d = new Dog(); // 可以访问 d.publicMethod(); // 可以访问 d.protectedMethod(); // 可以访问 d.defaultMethod(); // 可以访问 // d.privateMethod(); //私有的方法,不能被在类外访问 } }
//在上例的基础上,演示跨包的情况 package cat; //和dog 包不在一起 import dog.Dog; public class Cat { void test() { Dog dog = new Dog(); dog.publicMethod(); // 可以 // dog.protectedMethod(); 不可以 // dog.privateMethod(); 不可以 // dog.defaultMethod(); 不可以 } }##
//在前例的基础上,演示子类中的访问情况 class NiceDog extends Dog { void test() { Dog dog = new Dog(); dog.publicMethod(); // 可以 protectedMethod(); // 被protected修饰的成员,可以被继承过来,并这样调用 // dog.protectedMethod(); 不可以 // dog.privateMethod(); 不可以 // dog.defaultMethod(); 不可以 } }
The above is the detailed content of Detailed explanation of Java basic access permission examples. For more information, please follow other related articles on the PHP Chinese website!

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
