Inheritance, super, final, overwrite

Regarding inheritance, java only supports single inheritance and multiple inheritance;

				 即为一个类仅能有一个父类;

What is inheritance?

	继承可以提升代码的复用性(目的),父类的功能,子类可以直接使用;
																					  使子类的功能更加强大;
	java中用extends关键字表示继承;
	语法:public class 类名 extends 父类名{类体}

Note:
privatized attributes cannot be inherited;
if a class does not show that it inherits another class, the class inherits Object by default;
java.lang.Object is the ancestor class in java; it
means that the attributes in Object are available in all classes of;

super: maintains the characteristics of the parent type;

			作用:子类访问父类的属性

Super usage:

		1.用于成员方法/构造方法中区分子类和父类同名的成员属性;		
		2.用于子类构造方法中,调用父类构造方法;(必须出现在有效代码第一行)

Syntax: super (parameter);

Note:
①If there is neither this() nor super() in the subclass construction method;
then there will be a default super(); to call the parameterless construction of the parent class;
②this and super must appear in the construction method The first line, then it means that the two cannot appear at the same time;
③Neither this nor super can be used in static methods;

Summary:
Class name is used to distinguish static variables and local variables with the
same name ; this is used to distinguish member variables and local variables with the same name;
super is used to distinguish variables and methods with the same name from the molecular class and the parent class;
super: subclass construction method uses super (); To call the construction method of the parent class;
if the first line of the construction method does not display this() and super(), the default is that super() calls the parent class's no-parameter construction;
1. Create an object;
2. Initialize Parent attribute

Why must this() and super() appear in the first line of valid code in the constructor?

	 super():用来调用父类的构造方法,初始化父类属性,并创建对象;
	 最终一定会溯源调用到祖类Object,通过Object创建对象;
	 既然创建对象,就必须在构造方法第一行。因为构造方法有初始化成员属性的功能;
	 成员属性想要初始化,必须要有存储它的空间,而这个空间就在对象中;
	 所以要保证先有对象,再有数据初始化,所以super()必须在第一行;
	
	 this():重载调用当前类的其他构造方法;
				保证先有对象;

After the construction method is privatized, it cannot be inherited.
Although the subclass construction method, there will be a default super() to call the parameterless construction of the parent class;
but due to privatization, there is no access authority

Instance block

It can be regarded as a member method, which definitely needs to use an object;
but it has no name, so it cannot be called manually, but can only be called automatically;
when it is called, the object is created, and it is executed immediately;
therefore, the instance statement block is executed after the object is created. And before the execution of the construction method;

Overwrite: also known as rewrite, overwrite

Solve the problem that after the subclass inherits the parent class, some properties need to be different from the parent class;
①The child class has its own unique properties
②It can override the member methods of the parent class (override refers to member methods in particular)

Under what circumstances need to be overwritten?
If the method of the parent class can no longer meet the needs of the child class, the method in the parent class needs to be overwritten;
what are the conditions for overwriting ?
1. It must be in a system with inheritance;
2. The method name, return value type, and parameter list must all be the same;
3. The original method cannot be forced to have a broader exception (the exception is more specific);
4. It cannot be compared to the original method Have lower access rights (the lower the level, the higher the access rights);
5. Overwrite refers specifically to the overwrite of the parent class member method. The
most basic function of inheritance: code reuse; the
most important function of inheritance: the method can be overwritten, Polymorphism;
the purpose of overwriting?
1. To meet the current needs and modify the method body;
2. Errors are becoming less and less, more and more specific to a certain point, and there can be no wider exceptions;
3. The scope of use is getting wider and more levels. , Can not have lower access authority (the parent category has lower access authority than the child category access authority);
4. More and more powerful functions;

final: a modifier that means final, final, and unchangeable;

Note: ①Modified classes cannot be inherited;
②Modified variables are equivalent to constants and cannot be assigned twice (in the entire program life cycle);
there is no default value, and constants are generally public static final modified
③Modified member methods Cannot be overwritten;

In-depth final:

				final 修饰的变量,内存空间中的值不可更改;
				如果修饰的是引用类型变量,地址不能更改,
				但是与堆内存的内存空间无关(数据可以更改);

Guess you like

Origin blog.csdn.net/MIRACLE_Ying/article/details/112646485