Home > Java > javaTutorial > The relationship between Java function access modifiers and inheritance

The relationship between Java function access modifiers and inheritance

WBOY
Release: 2024-04-25 15:24:02
Original
1167 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 class Animal and a subclass Dog:

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’s name field uses the protected modifier, so ## The #Dog class has access to it. The eat()
  • method of
  • Animal uses the public modifier, so it can be called by both the Dog class and other classes it.
  • Dog's bark() method uses the public modifier, so any class can call it.

Note:

    Subclasses cannot access members of the parent class using the
  • private modifier.
  • Subclasses can change the value of parent class members with the
  • protected modifier.
  • 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template