Why can classes in java only be modified with public?

First of all, it is a false proposition that classes can only be modified with public. It should be said that we have only seen classes modified with public, and some classes do not have access modifiers. At this time, the access permission is default. Secondly, classes are actually divided into external classes and internal classes (inner classes). General classes are external classes, and internal classes need to be generated by external classes, and internal classes can be modified with various access modifiers. So the question is transformed into, why can't we use private and protected to modify external classes?

We know that in Java, access modifiers can modify classes, methods, variables, and construction methods.

There are 4 types of access rights from high to low

Inside the class This package Sub-package External packaging
public v v v v
protected v v v x
default v v x x
private v x x x

We use access modifiers to modify classes, just to allow classes to be accessed according to various permissions.

If the external class uses private modification, it cannot be accessed by other classes, and this class loses its meaning.

If the external class is decorated with protected, it seems that compared with the default, on the basis of the accessibility in the package, the subclasses outside the package are also accessible. However, if you want to become a subclass outside the package, you need to inherit the parent class first. However, the parent class cannot be found and cannot be inherited (the chicken or the egg first). The effect is the same as the default, and there is no need to exist.

About the inner class:

Inner classes are divided into member inner classes, local inner classes, anonymous inner classes and static inner classes.

A member inner class is a class defined in another class, and is the most common kind of inner class.

class Circle {
    
    
  double radius = 0;
   
  public Circle(double radius) {
    
    
    this.radius = radius;
  }
   
  class Draw {
    
       //内部类
    public void drawSahpe() {
    
    
      System.out.println("drawshape");
    }
  }
}

In this way, the class Draw is like a member of the class Circle, and Circle is called an external class. The member inner class can unconditionally access all member attributes and member methods of the outer class (including private members and static members).

The inner class can be modified with private, protected, default, public, and the private modification can only be accessed in the outer class.

About the interface

public interface Comparable<T>{
    
    
  int compareTo(T other);
}

All methods in the interface automatically belong to public, and there is no need to provide the keyword public when declaring methods in the interface.

Modified construction method

In the singleton design pattern, we use private to modify the construction method to privatize the constructor, which means that the class cannot be instantiated outside the class. After the class is instantiated, it provides a method for external access to return the instance object.

public class SingletonTest {
    
    
	public static void main(String[] args) {
    
    
		//测试
		Singleton instance = Singleton.getInstance();
		Singleton instance2 = Singleton.getInstance();
		System.out.println(instance == instance2); // true
		System.out.println("instance.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());
	}
}

//饿汉式
class Singleton {
    
    
	//1. 构造器私有化
	private Singleton() {
    
    
	}
	
	//2.本类内部创建对象实例
	private final static Singleton instance = new Singleton();
	
	//3. 提供一个公有的静态方法,返回实例对象
	public static Singleton getInstance() {
    
    
		return instance;
	}

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/114753097