Polymorphism, abstraction, interface

Polymorphism

Knowledge review: JVM features: cross-platform, multithreaded, object-oriented, automatic garbage collection mechanism
Object-oriented features: encapsulation, inheritance, dynamic (polymorphism), abstract
Variable declaration: data type variable name = value;
variable Classification: local variables; variables declared inside the method or objects declared by the parameter list;
static variables; variables modified with static;
member variables; variables not modified with static;

Knowledge points related to polymorphism: the
six principles of software design;
①The Richter replacement principle: where the parent class can be used, the child class can be used;
②Single responsibility principle: single function, only embrace one kind of change;
③Reliance inversion principle : High-level rely on the bottom through abstraction;
④Interface isolation principle: do not rely on interfaces that it does not need;
⑤Dimit’s principle: the principle of least knowledge, the less a class knows about other classes, the better, just like a person and relatives ,Friends communicate directly without talking to strangers;
⑥Opening and closing principle: open for extension, closed for modification;

What is polymorphism?
Polymorphism is a manifestation of the Richter substitution principle;
parent class references point to child class objects;
parent class references: reference type variables declared by the parent type;
references: who can be found through the variables of the entire reference type;
child class objects: instantiation The object, the subclass object declared with new;
polymorphism is: a reference type variable created by the parent class to find the object of the subclass declared with new;
usage: parent object variable name = new subclass ( ); For example: Animal a = new Cat ();

Can a statement situation be polymorphic?
Polymorphism can occur where there are variables, and polymorphism occurs at the time of assignment;
Animal a = new Cat();
Several forms of
polymorphism : ① Direct polymorphism; whether it is a member variable or a local variable, directly The declaration is polymorphic; parent class variable name = new subclass();
②formal parameters and actual parameters; when defining method parameters, use the parent class definition, and when calling the method, pass the subclass object;
③return value; return value type definition As the parent class, return the subclass object when returning data;

Disadvantages of polymorphism: loss of the unique properties of the subclass; that is, the properties of the subclass that the parent does not have;

Polymorphic call attributes: ①If the parent class does not have it, an error will be reported directly and cannot be accessed;
②If the subclass does not have it, all access to the parent class;
③If the parent class and subclass have both, except Member methods call subclasses, and others call parent classes; because member methods can be overridden;

The prerequisites for polymorphism:
① Must be in a system with inheritance relationship;
② Polymorphism is called up-casting; that is, the parent class can access the method of the child class inheriting the parent class and the extension of the parent class method (the parent class method Overwrite);
Polymorphism is also called up-casting, which is similar to the automatic type conversion of variables, the conversion from subclass to parent class;
Upcasting: can be regarded as forced type conversion, from parent class to subclass; the premise is that, There is an upward type conversion;

intanceof is used to determine whether the object is instantiated by a certain class;
the disadvantage of polymorphism: loss of subclass characteristics, need to be downcast and re-accessed; it
is a compile-time exception;

The life cycle of the program:
Compilation phase: During the compilation phase, the parent file and subclass files are present, but the subclass object does not exist, and the subclass object is generated at runtime;
we define polymorphism in the code and compile The processor will directly divide the space according to the type of the parent class, explain what is in the space, and check the grammatical structure; at
this time, only the attribute description in the parent class can be found, because the space is declared by the parent class;
runtime: the program starts running, each class Load the method area, start running, and execute new to create the object. The object is created with a subclass, so the object contains the unique attributes of the subclass;
but the type of the reference variable is the type of the superclass, and it has been statically bound at compile time, so use the space declared by the superclass When you can only find the attributes in the parent class;
therefore, the unique attributes in the subclass object cannot be found through the variables of the parent class;

Compile time:
When compiling, the properties of the parent class will generate a list of properties. At this time, the properties of the parent class already
exist . This is called binding at compile time. The program will be available after binding; the property value is not fixed. because the programs are not running these operations are done in the static area / zone method, the stack memory and heap memory without any incoming data;
bind this compile time, also known as static binding / binding before;
run time:
running time, to generate the corresponding object class, will also generate a list of attributes; case generated subclass instantiation object space with an initialization operation, the attribute value will have a subclass;
this in The data binding in the runtime phase is called runtime binding/dynamic binding/post-binding;
when calling: when
using the parent class reference to call the properties of the object of the subclass (the subclass's unique access is not accessible); it
can be understood as Use the attribute list of the parent class to call the attribute value list of the subclass (one-to-one correspondence, but it may be one-to-many, and the extra is the unique attribute of the subclass);

super: The official statement, representing the type characteristics of the parent class, which can be understood as the attribute list of the parent class;

Multi-state calls the results:
① no parent, no matter whether the sub-class are not called directly compile-time error;
② some parent class, sub-class does not, have run a parent;
③ parent class and subclass Both, except the member method calls the subclass, the other is the parent class;
implicit polymorphism
1. Direct assignment
2. Argument parameter
3. Return value
4. Implicit: similar to automatic type conversion

abstract

abstract is a modifier.
A class modified with abstract is an abstract class;
a method modified with abstract is an abstract method, and an abstract method has no method body; there is only a function definition, no function realization, and the function realization is completed by the subclass; the
realization of the abstract method is to The abstract method plus the method body, remove the abstract modification
. The purpose of the abstract class: the class cannot be instantiated, but can only be inherited; the
abstract method must be established in the abstract class; the
abstract class does not necessarily have abstract methods, such as Each class has objects and can be created as abstract methods;
final-modified classes cannot be inherited;
final-modified member methods cannot be overridden;
abstract classes are used to be inherited;
abstract methods are used to be overridden by implementation,
so fianl and abstract cannot appear at the same time

If a class wants to inherit an abstract class, it must implement all the abstract methods of the
abstract class; if an abstract class wants to inherit an abstract class, it can implement 0~N abstract methods;
although abstract classes cannot be created objects, they are abstract The class has a construction method, because the child class needs to call the parent class and then call the Object implementation to create an object;
abstract cannot modify static methods, because static methods cannot be overwritten;

Application scenarios of abstract classes:
For example, when we define the parent class, we only know the function of the parent class and don’t know how to implement it;
for example, animals can move; but the way of movement is different, such as flying, running, travel;
in this case, we define the function described as an abstract class, the specific implementation of the implementation class to complete;

Abstract methods are the same as member methods, except that there is no method body;
abstract methods must be overridden, and member methods are overridden according to requirements;

interface

Reference data types: class, array, interface

Interface is a kind of reference data type, which can be regarded as a special class. Java aims to solve the problem of weak function caused by multiple inheritance between
classes ; a class can only have one parent class, but it can have N Interfaces;

The way to create an interface, class changes to interface; inheritance relationship, extends changes to implements

Classes and classes use extends inheritance, which belongs to single inheritance;
classes and interfaces use implements for multiple implementations, separated by commas; for example, class A implements B, C, D.
Similarly, non-abstract classes implement interfaces and must implement all abstract methods ; The
abstract class implements the interface and can implement 0~N abstract methods

Interfaces and interfaces use extends, and are multiple inheritance; for example, interface A extends B,C,D

Syntax: public interface interface name {

		}

1. All variables in the interface are modified by public static final, that is, constants, which are usually omitted;
2. The methods in the interface are public abstract by default, that is, abstract methods, and they are agreed to be omitted;
3. Interface There is no construction method in it, and the object cannot be instantiated because it cannot establish a connection with the Object;

Since Java 1.8, there can be default methods and static methods in the
interface ; the interface can be regarded as a special abstract class, so in many cases, the interface and the abstract class can do a certain thing, the interface is preferred;
because the class and the interface It is multi-implemented. After using the interface, the inheritance of the class can be retained;

Guess you like

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