Java Foundation (object-oriented inheritance and polymorphism, selection and sorting of abstract classes, interfaces and arrays)

Posted by dougp23 on Fri, 14 Jan 2022 21:27:01 +0100

1. Code block

1. Classification of code blocks:

Local code block: a code block in a method in a class. Its function is to limit the life cycle of local variables;

Construction code block: it is defined outside the method in the class and wrapped with {}. Function: initialize data for some members in the class. Features: if there is a construction code block before executing the construction method, execute the content in the construction code block first;

Static code block: code block decorated with static; Features: load with class loading, prior to object existence; Static code blocks are executed only once during program execution;

Priority of code block: static code block > construction code block > local code block

2. How to generate document - html page

Open dos console

Enter the specified directory: javadoc -d folder name - author -version java file

2.this and super

this: represents the address value reference of the current class object
super: represents the spatial ID of the parent class (represents the address value reference of the parent class object)

Accessing member variables, member methods, construction methods
Member variables:
this. Variable name; It accesses the member variables of this class
super. Variable name; Accessing member variables of the parent class
Member method:
this. Method name (); Access member methods of this class
super. Method name (); Accessing member methods of the parent class
Construction method:
this() ; The parameterless constructor of this class is accessed
super() ; The parameterless constructor of the parent class is accessed

This (specific value); The parameter constructor of this class is accessed

Super (specific value); The parameterized constructor of the parent class is accessed

3. Succession

1. Concept of succession

Extract the contents of multiple classes into an independent class, so that the multiple classes and the independent class have a relationship - inheritance relationship

2. Benefits of inheritance:

1) improve code maintainability;

2) improve code reusability;

3) necessary conditions for polymorphism when making connections between classes

2. Characteristics of inheritance

1) Only single inheritance is allowed between classes, not multiple inheritance;

2) Multi level inheritance

3. Precautions for using inheritance

1) Subclasses inherit from the parent class and can inherit all non private properties and non private methods in the parent class, but private members can be accessed through public access methods;

2) Constructors cannot be inherited, but subclasses can indirectly access the constructors of the parent class through super

4. Relationship of each member variable in inheritance

1) The subclass inherits from the parent class. If the member variable name of the subclass is inconsistent with that of the parent class, you can access it separately;

2) If the member variable name of the subclass is consistent with that of the parent class

a) find it in the subclass first, and use it if any;

b) if not, look for it in the member variable of the parent class. If yes, use it. If not, report an error

5. Access to construction methods in inheritance

1) The subclass inherits the parent class, and all construction methods of the subclass will access the parameterless construction methods of the parent class;

2) If there is no parameterless constructor in the parent class, the subclass will report an error

resolvent;

1) the parameterless structure is always given in the parent class

2) the first sentence in the subclass construction method indirectly accesses the parametric construction method of the parent class through super;

3) as long as there is a method that can initialize the parent class in the construction method of the child class

6. When to use inheritance

Do not use inheritance relationships for partial functionality

Use inheritance relationship correctly: if class B is one of class A, inheritance can be used at this time

The essence of inheritance relationship: it embodies an "is a" relationship in real world things

Example 1:

//Define a mobile Phone class: Phone
class Phone{
    //phone
    public void call(){
        System.out.println("Mobile phones can make calls...");
    }
}
//New mobile phone class
class NewPhone extends  Phone{

    //As like as two peas, the subclass has the same method declaration as the parent class: the function of calling is used, and new functions are also needed.
    public void call(){

        System.out.println("You can watch the weather forecast...") ;
        //Follow the functions of the parent class
        super.call();

        System.out.println("You can also listen to music...") ;
    }
}
//Test class
public class ExtendsDemo2 {
    public static void main(String[] args) {
            //Create subclass objects
        NewPhone np = new NewPhone() ;
        np.call();
    }

Example 2:

Design a lamp class, in which the lamp has the attribute of bulb class and the method of turning on the lamp.

Design a bulb class. The bulb class has a shine method,

There are red bulb and green bulb

They all inherit a shiny method from the bulb class

//Design a lamp class, in which the lamp has the attribute of bulb class and the method of turning on the lamp.
public class Lamp {
    //Member variable
    Buble buble ;//Bulb class properties

    //Turn on the light --- does the bulb light up?
    //How to turn on the light
    public void on(Buble buble){ //Formal parameter -- passing a class -- calling the on method requires the creation of a light bulb class object
        buble.shine() ;

    }
}
//Design a bulb class. The bulb class has a shine method,
//There are red bulb and green bulb
public class Buble {
    //Method for lighting bulb
    public void shine() {
        System.out.println("The bulb can light up...");
    }
  
}
//Green bulb
public class GreenBuble extends Buble {
    @Override
    public void shine() {
        super.shine();
        System.out.println("And it can also emit green light");
    }
}
//Red bulb
public class RedBuble extends Buble {
    @Override
    public void shine() {
        super.shine();
        System.out.println("And it still glows red...");
    }
}
//Test class
public class Test2 {
    public static void main(String[] args) {

        //Create a table lamp class object
        Lamp lamp = new Lamp() ;
        //Call on light
        RedBuble redBuble = new RedBuble() ;
        lamp.on(redBuble); 
        System.out.println("---------------------------");
        lamp.on(new RedBuble());
        System.out.println("====================================");
        GreenBuble greenBuble = new GreenBuble() ;
        lamp.on(greenBuble);
        lamp.on(new GreenBuble());

    }
}

4. Polymorphism

Polymorphism is a kind of different state of things all the time

1. Prerequisites for polymorphism:

1) there must be inheritance relationship;

2) method override must exist;

3) there must be a parent class reference to a child class object

2. Polymorphic member access characteristics:

1) access to member variables: compile to the left and run to the left;

2) access to member methods: compile to the left and run to the right;

3) access to static member methods: compile to the left and run to the left;

4) for the construction method: hierarchical initialization, first initialize the parent class, and then initialize the child class

3. Benefits of polymorphism

1) It improves the reusability of code: guaranteed by inheritance;

2) The expansibility of the code is improved; Guaranteed by polymorphism;

4. Disadvantages of polymorphism

Cannot access subclass specific functions

resolvent:

1) create specific subclass objects for specific subclasses; (occupied memory)

2) use downward transformation;

Upward Transformation: Fu f = new Zi;

Downward transition: Zi = (Zi) f

5. Exception occurred during downward transformation of polymorphic usage

To use downward transformation, the premise is that there must be a parent class reference to a child class object, and the type of heap memory storage must be clear in mind

Improper use of downward transformation will lead to Java Lang.classcastexception: type conversion exception (belonging to runtime exception)

An exception occurs when an instance stored in heap memory is not of this type

5.final keyword

Final is a status modifier, which means final and cannot be changed

Characteristics of final keyword:

1) you can modify a class, which cannot be inherited

2) modify a member method, which cannot be overridden;

3) modify a variable, which becomes a constant

What is the difference between final modifying the basic data type and modifying the reference data type?

final modifies the basic data type: the corresponding data value of the basic data type cannot be changed;

final modifies the reference data type: the address value corresponding to the reference data type cannot be changed

6. Method rewriting and method overloading

1. Method Overload: Overload
Define multiple methods in a class. These methods have the same method name, different parameter lists, and have nothing to do with the return value;
Parameter list:
1) Different data types
2) Different number of parameters
3) Sequence

Purpose of overloading: to improve the extensibility of function: the same method can receive many types of parameters

2. Method Override: Override
In inheritance relationships, as like as two peas, the subclass is the same as the parent class. This subclass overwrites the parent class's function (copy / rewrite) to create a subclass specific object.
Directly method this function, use the function of the subclass!

Purpose of Rewriting: in order to follow the functions of the parent class and also use the functions of the child class (the specific child class has specific actions...)

7. Abstract class

Abstract keyword: abstract meaning

The purpose of abstract classes is to force subclasses to complete all abstract methods in the parent class

1. Format of abstract method:

Permission modifier abstract return value type method name (formal parameter list)

2. Format of abstract class

abstract class class name {}

3. Format of abstract method:

abstract void method name ();

Abstract methods cannot have method bodies

4. Characteristics of abstract classes:

1) Classes with abstract methods must be abstract classes;

2) Abstract classes do not necessarily have only abstract methods, but also concrete methods;

3) Abstract classes cannot be instantiated and need to be instantiated through specific subclasses (create objects);

4) There are two situations for subclasses of abstract classes:

a) if all subclasses of an abstract class are abstract classes, there must be a subclass that can instantiate the abstract class, otherwise the abstract class has no meaning;

b) the subclass of an abstract class is a concrete class, which can be instantiated directly

5. Member characteristics of abstract class:

Member variable: it can be either a variable or a constant (modified by final);

Member method: it can be defined as either abstract method or non abstract method;

If it is defined as an abstract method, use abstract to display the given;

Construction method: there is no parameter construction and parameter construction method (purpose: for hierarchical initialization);

Example 3:

//Parent class
abstract class Fu{
    //Member variable
    public int num = 10 ;

    //constant
    public final int num2 = 20 ;

    public Fu() {
        System.out.println("Fu...");
    }

    //Abstract method
    public abstract String show() ;  //abstract keyword cannot be omitted

    //Non abstract method
    public void method(){
        System.out.println("method Fu...");
    }


}
//Subclass
class Zi extends  Fu{

    public Zi() {
        System.out.println("Zi...");
    }

    int num = 20 ;

    @Override
    public String show() {
        return "show Zi...";
    }
}

//Test class
public class AbstractDemo2 {
    public static void main(String[] args) {
        //Abstract class polymorphism
        Fu fu = new Zi() ;
        System.out.println(fu.num);
        System.out.println(fu.num2);
        String zResult = fu.show();
        System.out.println(zResult);
        fu.method();
    }
}

Example 4:

/**
 * Employee class: contains the abstract method work() and the abstract method show()
 * 	    work()Method represents the work content
 * 		show()Method represents an introduction to employee properties
 * 		Programmers and project managers are employees
 * 		Programmer: attribute (name, job number, salary, bonus), behavior (work: Software Development)
 * 		Project Manager: attribute (name, job number, salary, bonus), behavior (work: control progress)
 *
 * 		Requirement: when subclasses are implemented, use system out. Println () is output on the console
 * 		For example: programmer work() output: "software development"
 * 	              show() Output: name is xxx, job number is xxx
 *
 */
//Employee / employee

public  abstract class Employee {

    //Extract the common content of programmer and manager classes into abstract classes
    private String name ; //full name
    private String empId ;//Job number
    private int salary ; //wages
    private int bonus ; //bonus

    public Employee() {
    }

    public Employee(String name, String empId, int salary, int bonus) {
        this.name = name;
        this.empId = empId;
        this.salary = salary;
        this.bonus = bonus;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
    //Contains the abstract method work() and the abstract method show()
    // The work() method represents the work content
    // The show() method represents the introduction of employee properties

    public abstract  void work() ; //Display work content
    public abstract  void show() ;//Display employee information introduction
}

//Manager class
public class Manager extends Employee {

    //Nonparametric / parametric construction method

    public Manager() {
    }

    public Manager(String name, String empId, int salary, int bonus) {
        super(name, empId, salary, bonus);
    }

    @Override
    public void work() {
        System.out.println("The main work is to control the project progress...");
    }

    @Override
    public void show() {
        System.out.println("Name is:"+super.getName()+",Job number is:"+super.getEmpId()+",Salary is:"+super.getSalary()+",Bonus is:"+super.getBonus());
    }

    //Unique function
    public void teach(){
        System.out.println("Teach programmers how to write requirements documents...");
    }
}

//Programmer class
public class Programmer  extends  Employee{

    //Nonparametric structure
    public Programmer() {
    }

    //Parametric construction method
    public Programmer(String name, String empId, int salary, int bonus) { 
        super(name, empId, salary, bonus);
    }

    @Override
    public void work() {
        System.out.println("The main work is software development...");
    }

    @Override
    public void show() {
        System.out.println("Name is:"+super.getName()+",Job number is:"+super.getEmpId()+",Salary is:"+super.getSalary()+",Bonus is"+super.getBonus());
    }

    //Unique function
    public void coding(){
        System.out.println("Programmers are equipped with motivators...");
    }
}
//Test class
public class Test3 {
    public static void main(String[] args) {
        //Abstract class polymorphism
        //Upward Transformation: direct assignment of parametric construction method
        Employee emp = new Programmer("****","9527",15000,2000) ;
        emp.show(); //Compile to the left and run to the right
        emp.work();
        //Downward transformation
        Programmer programmer = (Programmer) emp;
        programmer.coding() ;
        System.out.println("--------------------------------------------") ;
		//Upward transformation
        Employee emp2 = new Manager("******","9528",10000,2000);
        emp2.show();
        emp2.work();

        //Downward transformation
        Manager manager = (Manager) emp2;
        manager.teach();

    }
}

6. If there is no abstract method in a class, what is the significance of defining this class as an abstract class?

To prevent this class from being instantiated directly

How to instantiate:

1) there are specific subclasses directly

2) indirect has specific subclasses

Possible: the return value of a function is itself

7. What keywords conflict with abstract

private conflict:

Private methods can only be accessed in this class. The purpose of adding abstract is to be rewritten by subclasses and call the functions of subclasses

static conflict:

Static methods are loaded as classes are loaded, while static methods can only be loaded by class name The abstract method cannot have a method body, so there is a conflict

final conflict:

Member methods modified by final cannot be overridden, while methods added to abstract need to be overridden

8. Keywords that abstract can follow

public: default modifier;

Protected: protected (scope: current class or subclass under the same package or subclass under different packages)

9. Use of abstract

Use on a class: the class becomes an abstract class;

Use in method: the method becomes an abstract method;

Can be used in the interface

Example 5:

abstract  class Fu {

    //Define an abstract method
    abstract  String show() ;
    abstract  String method() ;
}

//Concrete subclasses override abstract methods in abstract classes
class Zi extends  Fu{

    @Override
    public String show() {
        return "hello,abstract" ;
    }

    @Override
    String method() {
        return "hello,javaEE" ;
    }
}

//Test class
public class AbstractDemo {
    public static void main(String[] args) {
        Fu fu = new Zi() ;
        String result = fu.show();
        System.out.println(result);

        String result2 = fu.method();
        System.out.println(result2);
    }
}

8. Interface

1. Essence of interface: it reflects the additional extended functions of a real world thing

2. Definition format: interface interface name {}

Methods in the interface cannot have a method body, that is, all methods are abstract methods, and all methods hide the public abstract keyword

3. Interface features:

1) cannot instantiate (cannot create an object), can only be instantiated through the sub implementation class of the interface (interface polymorphism);

2) how to instantiate:

Interface subclass instantiation: instantiate through the interface subclass (must be a concrete class);

Relationship between subclass and interface: implementation relationship

If the sub implementation class of the interface is an abstract class, there must be a concrete class of the abstract class, otherwise it cannot be instantiated

4. Naming rules for sub implementation classes of interfaces:

After the interface name + Impl: sub implementation class

//Interface
interface Inter{}
//Sub implementation class
class InterImpl implements Inter{}

Example 6:

//Define an interface:
interface  Jump{ //High jump interface

   /* public void jump(){
        System.out.println("You can jump ");
    }*/
   public abstract  void jump() ;
}

//Define a high jump cat class
class JumpCat implements Jump{ //Implementation relationship -- > keyword: implements

    @Override
    public void jump() {
        System.out.println("The cat can jump high...");
    }
}

//Test class
public class InterfaceDemo {
    public static void main(String[] args) {
        //Create interface object
        //Interface polymorphism: --- previous abstract class polymorphism: Fu Fu = new zi(); Fu class abstract class
        Jump jump = new JumpCat() ;
        jump.jump(); 

    }
}

5. Characteristics of interface members:

1) Member methods in the interface: can only be abstract methods. The default modifier is public abstract (it can be omitted when writing);

2) There is no constructor in the interface;

3) The member variable of the interface can only be a constant: there is a default modifier public static final (it can be omitted when writing)

Example 7:

interface Inter{

    //Define a member variable
    public static final int num = 10 ;
    public static  final  int num2 = 20 ;
	//Abstract method
    void show() ;
    public abstract  String method();
}

//Sub implementation class
class InterImpl implements  Inter{

    @Override
    public void show() {
        System.out.println("show InterImpl...");
    }

    @Override
    public String method() {
        return "method InterImpl...";
    }
}

//Test class
public class InterfaceDemo2 {
    public static void main(String[] args) {

        //Interface polymorphism
        Inter inter = new InterImpl() ;
        System.out.println(inter.num);
        System.out.println(inter.num2);
        System.out.println("----------------------");
        System.out.println(Inter.num);
        System.out.println(Inter.num2);
    }
}

6. Relationship between class and interface:

Class to class relationship: Extensions inheritance relationship

Only single inheritance is supported, not multiple inheritance, but multi-level inheritance can be supported;

Relationship between class and interface: implements relationship and implementation relationship

When a class inherits another class, it can implement multiple interfaces, separated by ",";

Interface to interface relationship: extends inheritance relationship

It supports not only single inheritance, but also multiple inheritance, separated by ","

Example 8:

/**Design a Java program
  		(1)Define an interface CanCry and describe the roaring method public void cry()
 		(2)Dog and Cat are defined respectively to implement CanCry interface. The functions of the implementation methods are:
  				Printout "I'm a dog, my bark is woof woof", "I'm a cat, my bark is meow meow"
 
		(3)Define a main class G,
  			Define a void makecry (cancry C) method, in which things that can roar roar.
  			Create dog object (dog), cat object (CAT) and G object (g) in main method
 		g Call the makecry method to make dogs and cats roar.
*/
//Roaring interface
 public interface CanCry {

    public void cry() ;
}
//Cats
public class Cat  implements CanCry{
    @Override
    public void cry() {
        System.out.println("I'm a cat. My cry is meow meow") ;
    }
}
//Dogs 
public class Dog implements  CanCry{
    @Override
    public void cry() {
        System.out.println("I'm a dog. My bark is barking");
    }
}
//Test class
public class G {
    public static void main(String[] args) {
        Dog d = new Dog() ; //A dog is a dog
        Cat c = new Cat() ;

        G g = new G();
        //g.makeCary(new CanCry()); // Cannot instantiate
        g.makeCary(d);  //CanCry c = new Dog() ;   Interface polymorphism
        g.makeCary(c) ; //CanCry c = new Cat() ;

        System.out.println("---------------------------------");
        //Anonymous object
        g.makeCary(new Dog());
        g.makeCary(new Cat());

        System.out.println("-----------------------------------");
        //Chain programming: play by yourself
        new G().makeCary(new Dog());
        new G().makeCary(new Cat());

    }


    //The formal parameter of the method is the interface type -- > which requires the sub implementation class object of the interface
    void makeCary(CanCry c){ //Default repair---

        c.cry();
    }
}

9. Difference between interface and abstract class

1) Differences in membership
Abstract class:
Member variable: it can be either a variable or a constant
Member method: there can be abstract methods (must carry the abstract keyword) or non abstract methods
Construction method: there are parameterless / parameterless construction methods. The purpose is to initialize the data and initialize it hierarchically
Interface:
Member variable: can only be a constant. There is a default modifier public static final
Member methods: can only be abstract methods: there is a default modifier public abstract
Construction method: None

2) Relationship differences
Between classes: inheritance relationship. Only single inheritance is supported. Multiple inheritance is not supported, but multi-level inheritance is allowed
Class and interface: implementation relationship. A class can implement multiple interfaces while inheriting another class, separated by commas
Interface and interface: inheritance relationship. It can support single inheritance, multi inheritance, multi-layer inheritance

3) Design concept
Abstract classes: --- > things that must be completed by mandatory subclasses. Due to the inheritance relationship, it reflects a "is a" relationship!
Interface ------ > reflects the additional function of a certain thing. Whoever implements the interface has the function, which reflects a "like a" relationship!

10. Selection and sorting of arrays

The idea of selecting sorting: compare in sequence: use the elements corresponding to the 0 corner mark to compare the elements corresponding to the following corner mark in sequence. Put the small one forward. After the first comparison, the minimum value appears at the minimum index... Compare in this order

//Select the core code to sort:
for(int x = 0 ; x < arr.length-1; x ++){
    for(int y = x + 1;  y< arr.length ; y ++){
        //judge
        if(arr[y] < arr[x]){
            int temp = arr[x] ;
            arr[x] = arr[y] ;
            arr[y] = temp ;
        }
    }
    
}

11. Compile under the same package and different packages

1. Compilation and operation under different packages
1) Net hsbc. Import demo class
2) Net. Net is automatically hsbc. Demo - > compile into
net
hsbc
Demo.class
3) Then test Compile java files
javac -d . Test.java —
com
qf
Test.class

4) run java.com qf. Test

2. Compilation and operation under the same package:

	The first way:
			Manual mode:
			1)You need to add com Folders and subfolders qf Create it manually
			2)get into dos Console:Enter current java The directory where the file is located,javac  Source file name(HelloWorld.java)---->
						HelloWorld.class Bytecode file
						
			3)Will 2)Put the bytecode file generated in step 1)qf Under subfolders
			4)function:Run with package :java Package name.Class name
					java com.qf.HelloWorld
					
	The second way:
			Automatic mode:
			1)Direct entry dos Console,Enter the specified directory:
					javac -d . java source file(HelloWorld.java)
			2)Automatically create the package name and the bytecode file under the package	
			3)function:Run with package :java Package name.Class name

12. Formal parameters

Research on formal parameter problem: reference type

If the formal parameter of a method is a method called by a class, how to pass the actual parameter?

The specific parameters of the current class need to be passed

Abstract class, the subclass object of the abstract class that needs to be passed when calling the actual parameters of the method (abstract class polymorphism)

When the interface calls this method, the actual parameters need to be passed are the sub implementation class objects of the current interface (interface polymorphism)

14. Return value problem

If the return value of a method is a reference type and the final method ends, how to return?

Concrete class: the method returns the current concrete class object!

Abstract class: what needs to be returned is the subclass object of the abstract class

Interface: what needs to be returned is the sub implementation class object of the interface

15. Internal class

1. What is an internal class?

Another class is defined in one class:

If class B is defined in class A, class B is called the inner class of class A, and class A is the outer class;

Member inner class:

Define another class in the member position of one class;

Internal classes can access members of external classes, including private classes

Example 9:

//External class
class Outer{
    //Member variable
    public int num = 100 ;
    private int num2 = 200 ;

    class Inner{ //Member inner class
        //A member method of a class within a member
        public void method(){
            System.out.println("method Inner");
            System.out.println();
            System.out.println(num2);
        }

    }

    //Member methods of external classes
    public void show(){
        //Method of the internal class of the accessed member --- > accessed by creating an internal class object
      
        Inner inner = new Inner() ;
        inner.method();
    }


}

public class InnerClassDemo {
    public static void main(String[] args) {
        //Create an external class object
        Outer outer = new Outer() ;
        outer.show();
    }
}

2. How do external classes directly access member methods of internal classes?

Format: external class name Internal class name object name = external class object Internal class objects;

Example 10:

class Outer2{
    private int num = 20 ;

    //Member inner class
    class Inner2{
        public void show(){
            System.out.println(num);
        }
    }

    //Member methods of external classes
    public void method(){
        // Create an inner class object to access the member methods of the inner class
    }
}

//Test class
public class InnerClassDemo2 {
    public static void main(String[] args) {

        //External class name Internal class name object name = external class object Internal class objects;
        //Applicable to: directly accessing members of the internal class of a member through an external class (prerequisite: the internal class of the current member is a non static class)
        Outer2.Inner2 oi = new Outer2().new Inner2() ;
        oi.show() ;
    }
}

3. How do external classes access members of static members and internal classes?

The static member inner class is regarded as the static member access of the outer class

Direct access:

External class name Internal class name object name = new external class name Internal class name ();

Example 11:

class Outer3{
    //Define non static member variables
    public int num = 50 ;
    private static int num2 = 20 ;

    //Define member inner class: static ----- > static member inner class can be regarded as static member of external class
    static class Inner3{//At this time, all classes are static
        public void show(){
            System.out.println(num2);
        }
        public static void show2(){
            System.out.println(num2);
        }
    }
}


//Test class
public class InnerClassDemo3 {
    public static void main(String[] args) {

        //   External class name Internal class name object name = new external class name Internal class name ();
        Outer3.Inner3 oi = new Outer3.Inner3() ;
        oi.show();

        System.out.println("------------------------------");
        //Another way of show2()
        Outer3.Inner3.show2(); //show2() static method

    }
}

16. Permission modifier

Permission modifier:

Default modifier

Private modifier: private

Protected: protected

Public: public

In the current class under the same package, in the subclass under the same package / in the subclass under different packages in the unrelated class

private √

Default modifier √

protected √ √ √

public √ √ √ √

Modify permissions from small to: private, default, protected,public