Home> Java> javaTutorial> body text

The relationship between Java function access modifiers and inheritance

WBOY
Release: 2024-04-25 15:24:02
Original
1084 people have browsed it

Access permission modifiers determine the access scope of classes, methods and fields, and play an important role in inheritance: public: Allow access to all classes and subclasses. protected: allows access by classes and their subclasses in the same package. default: Allow access to classes within the same package. private: Allows access only to the class itself that defines the modifier. A subclass cannot access the members of the parent class using the private modifier, but can change the value of the parent class member with the protected modifier. If you do not specify an access modifier, the member will default to default (package scope).

Java 函数的访问权限修饰符之与继承的关系

The relationship between access modifiers of Java functions and inheritance

The access modifiers in Java determine classes and methods and field access scope play an important role in inheritance. The following is the relationship between access modifiers and inheritance:

  • public:Grants access to all classes and subclasses.
  • protected:Grants access to classes and their subclasses in the same package.
  • default (package scope):Grants access to classes in the same package.
  • private:Grants access only to the class itself in which this modifier is defined.

Practical case:

Suppose we have a parent classAnimaland a subclassDog:

public class Animal { protected String name; public void eat() { System.out.println("Eating..."); } } public class Dog extends Animal { public void bark() { System.out.println("Barking!"); } }
Copy after login

In the above code:

  • Animal’snamefield uses theprotectedmodifier, so ## The #Dogclass has access to it. Theeat()
  • method of
  • Animaluses thepublicmodifier, so it can be called by both theDogclass and other classes it.
  • Dog'sbark()method uses thepublicmodifier, so any class can call it.

Note:

    Subclasses cannot access members of the parent class using the
  • privatemodifier.
  • Subclasses can change the value of parent class members with the
  • protectedmodifier.
  • If no access modifier is specified, the member will default to
  • default(package scope).

The above is the detailed content of The relationship between Java function access modifiers and inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!