Java - Modifier Types
@reference_2_tutorialspoint
Java - Non Access Modifiers
Access Control and Inheritance
The following rules for inherited methods are enforced −- Methods declared public in a superclass also must be public in all subclasses.
- Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
- Methods declared private are not inherited at all, so there is no rule for them.
@reference_3_tutorialspoint
Java - Access Modifiers
Access Modifiers and Inheritance
When you create a subclass of some class, the methods in the subclass cannot have less accessible access modifiers assigned to them than they had in the superclass. For instance, if a method in the superclass ispublic
then it must be public
in the subclass too, in case the subclass overrides the method. If a method
in the superclass is protected
then it must be either protected
or public
in the subclass.
While it is not allowed to decrease accessibility of an overridden method, it is allowed to expand accessibility of an overridden method. For instance, if a method is assigned the default access modifier in the superclass, then it is allowed to assign the overridden method in the subclass the
public
access modifier.Class Access Modifiers
It is important to keep in mind that the Java access modifier assigned to a Java class takes precedence over any access modifiers assigned to fields, constructors and methods of that class. If the class is marked with thedefault
access modifier, then no other class outside the same Java package can access that class,
including its constructors, fields and methods. It doesn't help that you declare these fields public
,
or even public static
.
The Java access modifiers
private
and protected
cannot be assigned to a class.
Only to constructors, methods and fields inside classes. Classes can only have the default (package) and public
access modifier assigned to them.
@reference_4_jenkov.com
Java Access Modifiers
why a class cannot be defined as protected?
Because it makes no sense.
Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.
Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing.
You can declare nested and inner classes as protected or private, though.
@reference_5_stackoverflow
why a class cannot be defined as protected?
@reference_6_quora.com
Why can't we declare class as protected in Java?
Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.
Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing.
You can declare nested and inner classes as protected or private, though.
@reference_5_stackoverflow
why a class cannot be defined as protected?
@reference_6_quora.com
Why can't we declare class as protected in Java?
No comments:
Post a Comment