Java foundation notes

Object oriented construction method

In Java, there is a special method called construction method, also known as constructor or constructor. By providing this constructor, we can ensure that every object is initialized. Of course, our construction method can only be called during the creation time of the object, so as to ensure our initialization. Our construction method is special. It has no parameter type and return value. Its name should be consistent with the class name.

class Apple{
	int sum;
	String color;

	public Apple(){}
	public Apple(int sum){}
	public Apple(String color){}
	public Apple(int sum, String color){}

An Apple class is defined above. We can find that the Apple class has no parameter type and return value, and there are multiple methods with the same name as Apple, and the parameter lists of each Apple are different. In fact, this is a manifestation of polymorphism.

class createApple {
	public static void main(String[] args) {
		Apple apple1 = new Apple();
		Apple apple2 = new Apple(1);
		Apple apple3 = new Apple("red");
		Apple apple4 = new Apple(2,"color");

If we define four Apple objects and call four different construction methods of Apple, the construction method without any parameters is called the default construction method, and the construction method without any parameters is called the default construction method. If no construction method is defined in the class, the JVM will automatically generate a construction method, It should be noted that if we manually define any construction method, the JVM will no longer provide you with a default constructor. At this time, we must specify it manually, otherwise a compilation error will occur
The error shown is that we have to provide an Apple constructor with an int parameter, and the default parameterless constructor function is not allowed.

Method overloading

Another important concept in Java is method overloading, which is a different form of class name. In fact, the constructor of Apple mentioned just now is also a kind of overloading. Therefore, each overloaded method has a unique parameter list, including parameter type, order and number of parameters.
Overload conditions:

  • Method names must be the same
  • The parameter list must be different (number, type, and order of parameter types are different)
  • The return types of methods can be the same or different
  • Simply having different return types is not enough to be called method overloading
  • Overloading occurs at compile time, so the compiler can choose which method to use according to the type of parameter

Method rewrite

Although the names of rewriting and overloading are similar, they are actually completely different. The description of method rewriting is between the child class and the parent class, while overloading only refers to the same class.

class Fruit{
	public void eat(){
		System.out.println("eat fruit");

class Apple extends Fruit{
	public void eat(){
		System.out.println("eat apple");

Rewriting principle

  • The overridden method must be consistent with the parent class, including return type, method name and parameter list
  • Overridden methods can be identified with the * * @ Override * * annotation
  • The access permission of overridden methods in a subclass cannot be lower than that of methods in the parent class


Class initialization

In fact, when we use the new keyword to create an object, we call the object parameterless constructor for initialization. This parameterless constructor can be hidden and automatically added by the JVM, that is, the constructor can ensure class initialization.

Initialization sequence

  • Static attribute: the attribute defined at the beginning of static
  • Static method block: a block of code wrapped in static {}
  • Common attribute: non static defined attribute
  • Common method block: {} wrapped code block
  • Constructor: method with the same class name
  • Methods: common methods
public class LifeCycle {
	// Static properties
	private static String staticField = getStaticField();
	// Static method block
	static {
		System.out.println("Static method block initialization");
	// General properties
	private String field = getField();
	// Common method block
	// Constructor
	public LifeCycle() {
		System.out.println("Constructor Initializers ");
	public static String getStaticField() {
		String statiFiled = "Static Field Initial";
		return statiFiled;
	public static String getField() {
		String filed = "Field Initial";
		return filed;
	// Main function
	public static void main(String[] argc) {
		new LifeCycle();

Variable parameter list

A popular use of arrays in Java is variable parameters

public int add(int... numbers){
	int sum = 0;
	for(int num : numbers){
		sum += num;
	return sum;
add(new Integer[] {1,2,3}); //Pass array

Destruction of objects

Although the Java language is based on C + +, an important feature of it and C/C + + is that it does not need to manually manage the destruction of objects.
In Java, we no longer need to manually manage the destruction of objects. It is managed and destroyed by the Java virtual machine. Although we don't need to manage objects manually, we need to know the concept of object scope.

Access control rights

There are four kinds of access permissions for members in Java: public, protected, default and private


Inheritance is an integral part of all OOP and Java languages. As long as we create a class, we implicitly inherit from the parent class of Object, which is called implicit assignment, and extensions is the display assignment.


Polymorphism means that the same behavior has multiple different manifestations. It refers to that the same method of a class instance (object) has different manifestations in different situations. Encapsulation and inheritance are the basis of polymorphism.
Three conditions for the realization of polymorphism

  • inherit
  • Override parent method
  • A parent class reference points to a child class object


Composition is to put the object reference in a new class. Composition is also a way to improve the reusability of classes. If you want the class to have more extended functions, you need to use more combinations and less inheritance.


Proxy is roughly that a wants to call the method of class B, but a will not call it directly. A will create a proxy of object B in its own class, and then call the method of B.

public class Destination{
	public void todo(){

public class Device {
	private String name;
	private Destination destination;
	private DeviceController deviceController;
	public void control(Destination destination){

public class DeviceController{
	private Device name;
	private Destination destination;
	public void control(Destination destination){

Upward transformation

Upward transformation represents the relationship between parent and child classes. In fact, there are not only upward transformation but also downward transformation between parent and child classes. Their scope after transformation is different.

Tags: Java Back-end

Posted on Thu, 28 Oct 2021 13:03:56 -0400 by PJ droopy pants