[Java Silver] Summary of access modifier points

Access modifier

Access modifier Accessable Symbols in class diagrams
public From anywhere +
protected Classes and subclasses in the same package #
Classes in the same package ~
private Current class -

Access from an unauthorized location will result in a ** compilation error **

What you need to consider access modifiers

・ ** Class ** (to determine if it can be accessed from another package) ・ Method -Collection (array, ArrayList) ·field

Notes on access modifiers

Interface and access modifiers

Since the interface is ** a prerequisite to be inherited **, the interface itself is implicitly considered ** public ** → Methods that can be defined in the interface -In the case of ** abstract method **, it is implicitly interpreted as ** public , abstract (public, abstract can be omitted). -If ** has an implementation, it must be ** static method ** or ** defalt method ** ( access modifiers are unlimited, private is also possible **)

→ Fields that can be defined in the interface -Static variables (** public only ) ・ Constant ( public only **) -Instance variables cannot be defined

** [Concrete method of class that implements interface] **

The access modifier of the abstract method ** defined in the interface and the concrete method ** that implements the abstract method in the class that implements the interface must be ** the same access modifier **. ..

Example) * A compilation error has occurred

interface A(){
  void doSomething(); //Inderface abstract method. Implicitly interpreted as public
}

B implements A(){
  protected void doSomething(){ //Compile error. Access modifier must be "public", same as the interface abstract method
   //processing
  }
}

Abstract classes and access modifiers

Abstract classes (qualified with abstract) are implicitly considered ** public ** because abstract classes are ** inherited assumptions **

→ Methods that can be defined in abstract classes -For abstract methods, qualification with ** abstract ** is required. Since the abstract method is a method of ** premise to be overridden **, ** access modifier can be set other than private ** -For concrete methods, access modifiers can be set freely.

→ Fields that can be defined in the abstract class ・ No restrictions on settings

** * The access modifier when overriding the method should be the same as the original method or the restrictions should be relaxed **

Example)

interface A {
    void doSomething();
    
}

abstract class B implements A {
   public abstract void doSomething(); //The interface can be implemented with abstract methods. Access modifiers are the same as interface methods
}

class C extends B {
    public void doSomething(){ //Override abstract method.
        System.out.println("Do something");
    };
}

public class Main {
	public static void main(String[] args) {
		C example = new C();
		example.doSomething();
	}
}

Recommended Posts

[Java Silver] Summary of access modifier points
[Java Silver] Summary of points related to lambda expressions
Summary of [Java silver study] package
Access modifier [Java]
Summary of Java support 2018
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of knowledge required to pass Java SE8 Silver
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[For beginners] Summary of java constructor
AtCoder 400 points algorithm summary (Java edition)
Summary of object-oriented programming using Java
Summary of in-house newcomer study session [Java]
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
Summary of Java Math.random and import (Calendar)
Summary of good points and precautions when converting Java Android application to Kotlin
Java knowledge summary
[java] Summary of how to handle character strings
Java Generics Summary
Summary of Java environment settings for myself [mac]
Java Silver memo
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
Summary of points I was worried about when migrating from java to kotlin
[Java] Access the signed URL of s3 (signed version 2)
Java related summary
[Java SE 11 Silver] Arrays class method summary [Java beginner]
Java 8 documentation summary
[Qualification Exam] Java SE 8 Silver Learning Method Summary
Java 11 document summary
Study Java Silver 1
[Java] Overview of Java
java final modifier
This is convenient! Summary of popular Java library + α
Omission of curly braces in if statement (Java silver)
Summary of ToString behavior with Java and Groovy annotations
Summary of revisions (new era) support by Java version
Access modifier summary-JVM language edition (Java / Groovy / Scala / Kotlin)
[java.io] Summary of Java string input (InputStream, Reader, Scanner)
Experience of passing Java Silver as a new graduate
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
Story of passing Java Silver SE8 (Oracle Certified Java Programmer, Silver SE 8)
Summary of file reading method for each Java file format
Expired collection of java
Java abstract modifier [Note]
Predicted Features of Java
Java 12 new feature summary