1 - inheritance, template design pattern, abstract class, final

Keywords: Java JavaEE

preface

Tip: here are the knowledge points of inheritance, abstract classes, template design patterns and final keywords

Tip: the following is the main content of this article. The following cases can be used for reference

1, Inherit

Overview: a class inherits from another class. This class is a child class, and the inherited class is the parent class.
Benefits: ① improve the reusability of code, ② create a relationship between classes

1. Inherited format

Modifier subclass name extends Parent class name{}
public  Zi 	 extends  Fu{}

2. Access rules for inherited members

2.1. Access rules of construction methods after inheritance:

The subclass cannot inherit the constructor of the parent class;

2.2. Access rules for private members after inheritance:

After inheritance, the child class cannot directly access the private members of the parent class, but can access them indirectly;

2.3. Access rules for non private members after inheritance:

After inheritance, the subclass can directly access the non private members of the parent class;
Access rules:
	Find in subclasses first. If found, use the members of subclasses directly
		If it is not found, search in the parent class;
		

3. Three uses of this and super keywords

3.1 three uses of this:

Introduction: this can access member variables, member methods and construction methods;

①,Access member variables:
	this.Member variable name;
	Usage scenario: distinguish local variables and member variables with the same name
	
②,Access member method:
	this.Member's legal name(Argument);
	
③,Access construction method:
	this(Argument): Another construction method can be invoked in one construction method in this class.
be careful:
	1,Other construction methods of this class can only be invoked in this class's construction method.
	2,this The constructor calling this class can only be placed on the first line of the constructor;
	3,Two constructors of this class cannot call each other
	

3.2. Three uses of super:

Introduction: super can access the member variables, member methods and construction methods of the parent class;

①,Access member variables:
	super.Member variable name;
	Usage scenario: distinguish between child and parent class member variables with the same name
	
②,Access member method:
	super.Member's legal name(Argument);
	Usage scenario: distinguish between child and parent class member methods with the same name;
	
③,Access construction method:
	super(Argument)
be careful:
	1,super The constructor accessing the parent class must be placed in the first line of the constructor of the child class;
	2,The subclass constructor will call the null parameter constructor of the parent class by default;
	3,In the subclass construction method, calling the parent class construction method is in order to initialize the attributes inherited from the parent class.

super Precautions for use:
	1,super Access member variables and member methods, and find them in the parent class first. If not, find them in the parent class. If not, so on;
	   Java The top-level parent class is Object,All classes inherit directly or indirectly Object Class;
	2,The constructor of the subclass will call the null parameter constructor of the parent class by default. If only the parametric constructor is defined in the parent class, but no null parameter constructor is defined, the compilation will report an error;
	

4. Method override

Overview: the subclass has the same method as the parent class (the return value type, method name and parameter list are consistent). This method is the method that overrides the parent class;

Notes for rewriting:
	1,Method rewriting occurs in parent-child classes;
	2,For the overridden method, its return value type, method name and parameter list are consistent;
	3,use@Override Annotation, you can check whether the rewriting is successful and verify the rewriting annotation;
		It is recommended to add this annotation to the rewriting methods, which can improve the readability of the code and prevent errors on the one hand;
	4,The overriding method of child and parent classes is to follow that the permission of the child class is greater than or equal to that of the parent class
	5,Usage scenario: if the methods of the parent class and the subclasses have different implementations, the subclasses need to override the methods of the parent class;

5. Characteristics of inheritance

1,Java Inheritance only supports single inheritance, not multiple inheritance;
class A { }
class B { }
class C1 extends A {
	// class C2 extends A, B {// error }
}
2,A class can only have one parent class and can have multiple subclasses;
class A { }
class c1 extends A{}
class c2 extends A{}

3,Multi level inheritance
class A { }
class B { }
class A extends B{}
class B extends C{}

2, Abstract class

1. Definition and use of abstract classes

Overview: there is no method body, and the class modified by abstract is an abstract class;
characteristic:
① . classes with abstract methods must be abstract classes, and abstract classes do not necessarily have abstract methods;
② . force subclass override

Format:
	Modifier  abstract class Class name{}
	public abstract class Fu{}

Members in abstract classes:
	Member variable
	Member method
	Construction method
	Abstract method

Precautions for abstract classes:
	1,An abstract class cannot be created as an object. It can only be a parent class and inherited by subclasses; It embodies the idea of template design;
	2,Abstract classes cannot be created objects, but they can have constructors----->Initialize the attributes inherited from the parent class;
	3,There can be no abstract method in an abstract class, but the abstract method must be defined in the abstract class
	4,After a subclass inherits an abstract class, it must override all abstract methods in the abstract class, otherwise the subclass must also be an abstract class;
	

2. Definition and use of abstract methods

Format:
	Modifier  abstract Return value type method name(parameter);
	public abstract   void    method(parameter);
	

Three. Template design mode:

Design mode:
Design pattern is a fixed idea to solve some problems, that is, a summary of code design ideas;

Template idea:
	1,Templates are common things, and abstract classes embody the idea of templates;
	2,Abstract classes can be concrete methods(global template),It can also be an abstract method(Fill template);
	3,Behaviors that cannot be determined in the template are defined as abstract methods, so that subclasses of the template need to be used to inherit and rewrite the implementation;
	4,The behaviors that can be determined in the template are defined as methods with method bodies, which can be directly inherited and called by subclasses that need to use the template;
	
Case:
	demand:According to the following requirements, it is implemented with code 
	Define new and old drivers. Both new and old drivers have driving functions. The driving steps are the same, but the driving posture is different 
	New driver:Open the door,ignition,Hold the steering wheel with both hands,brake,Flameout 
	Old driver:Open the door,Ignite,Hold the steering wheel with your right hand and smoke with your left hand,brake,Flameout

public abstract class Driver{
	public void driver(){
		//Short for System.out.println()
		sout("Open the door..");
		sout("ignition..");
		zishi();
		sout("brake..");
		sout("Flameout");
	}
	public abstract void zishi();
}

class newDriver extends Driver{
	@Override
	public void zishi(){
		sout("Hold the steering wheel with both hands");
	}
}

class oldDriver extends Driver{
	@Override
	public void zishi(){
		sout("Hold the steering wheel with your right hand and smoke with your left hand");
	}
}
	

4, final keyword

Overview: final is a keyword that means final and immutable; It can be used to modify variables, methods and classes;

1. final modifier member variable

Format:
	Modifier  final int num = 10;
	public final int num; num = 10;
characteristic:
	1,cover final Modified variables can only be assigned once and cannot be assigned repeatedly;
	2,Generally, in development, it is final All modified variables need to be capitalized;(final Modified variables are actually custom constants);
	

2. final modifier member method

Format:
	Modifier  final return type method(parameter list ){Method body....}
	public final   void    method(parameter list ){Method body...}
characteristic:
	cover final Modifier methods cannot be overridden;
	

3. final modifier class

Format:
	Modifier  final class Class name{}
	public final class Fu{}
characteristic:
	cover final Modified classes cannot be inherited;
	

The follow-up basic content of java is being updated quickly. If the content is wrong, all coders will put forward constructive suggestions.

Posted by AwptiK on Sat, 18 Sep 2021 03:17:27 -0700