Sobes.tech
Junior — Middle

Selgitage kaitsekatte kasutamise eesmärki ja kasutamist objektorienteeritud programmeerimises.

sobes.tech AI

Vastus AI-lt

The protected access modifier in object-oriented programming restricts access to class members (fields, methods) so that they are accessible:

  • Within the class itself,
  • In derived classes,
  • Within the same package (in languages that have the concept of packages, e.g., Java).

This allows hiding implementation details from the outside world, while still permitting subclasses to use and override these members.

Example in Java:

public class Animal {
    protected void makeSound() {
        System.out.println("Some sound");
    }
}

public class Dog extends Animal {
    @Override
    protected void makeSound() {
        System.out.println("Bark");
    }
}

Here, the makeSound method is accessible within the Dog class, but not from other classes that are not subclasses.