Repository navigation
Open
Description
상속이란?
상속을 하는 이유?
protected 접근제어자가 필요한 이유
The protected access modifier in Java is used to provide a level of access control between classes within the same package and classes in subclasses. The protected modifier allows a subclass to access the members (fields and methods) of its superclass that are marked as protected, while still maintaining some level of access restriction compared to the public modifier.
Here are some reasons why the protected access modifier is necessary in Java:
Encapsulation: The protected modifier allows you to maintain the integrity of the class by hiding its internal details from other classes in the same package, while still allowing subclasses to access the necessary parts of the superclass. This helps to maintain the encapsulation of the class and reduces the risk of unintended side effects from other code.
Reusability: The protected modifier allows you to create reusable code by creating a class hierarchy. By making members protected, you can ensure that subclasses can access and extend the functionality of the superclass, while still maintaining encapsulation.
Polymorphism: The protected modifier allows you to use polymorphism in your code by creating a hierarchy of classes that share a common interface. Polymorphism allows you to write generic code that can handle objects of different classes in a type-safe manner.
In summary, the protected access modifier provides a balance between access control and code reuse, making it a valuable tool for creating robust, maintainable, and reusable Java code.
Metadata
Metadata
Assignees
Labels
No labels
Activity