3. constructor:
When initializing a subclass object, the constructor of the parent class also runs. That is
This is because the first line of the subclass constructor has an implicit statement super () by default ();
Super () accesses the constructor of the null parameter of the parent class. And all the sub-classes
The first line of the constructor is super () by default ();
Why must the subclass access the constructor in the parent class?
Because the data subclass in the parent class can be directly obtained, when the subclass object is created
First, you need to check how the data is initialized when the parent class is used. So subclass
When initializing, you must first access the constructor in the parent class.
To access the constructor specified in the parent class, you can manually define super
Statement.
Note: The Super statement must be defined in the first line of the subclass constructor.
Subclass instantiation process.
By default, all constructors of the subclass access the constructor of the null parameters of the parent class.
Quantity
Because the first line in each constructor of the subclass has an implicit super ();
When the parent class does not have a constructor with null parameters, the subclass must manually pass the super
Or use this statement to specify the constructor to access the subclass.
Of course: You can manually specify this statement to access the first line of the subclass constructor.
Constructor in this class. At least one constructor In the subclass accesses the parent class.
Constructor.
Final (final) keyword
* Final can modify classes, methods, and variables.
* Final-modified classes cannot be inherited. In order to avoid inheritance, quilt duplication
Function.
* The final modification method cannot be overwritten.
* The final variable is a constant. It can be assigned only once. Can be modified
Member variables, and can modify local variables.
When describing a thing, the appearance of some data is fixed.
Strong Readability: give names to these values for ease of reading. This value is not required.
To change, add final modification. As a constant: writing standard of constants
Uppercase letters exist. If multiple words are used, the words are connected "_".
.
* Internal classes can only access local variables modified by final.
When an internal class is defined on a local position in the class, it can only access the local final
Local variable.
Abstract: general, fuzzy, and non-specific.
When multiple classes have the same function but different feature subjects, you can also
. In this case, only function definitions are extracted, but function subjects are not extracted.
Abstract methods are stored in abstract classes.
Abstract: it cannot be understood.
Abstract class features:
1. abstract methods must be defined in abstract classes.
2. Abstract methods and abstract classes must be modified by abstract keywords.
3. abstract classes cannot use new to create objects because abstract methods are called.
No.
4. abstract methods in abstract classes must be rewritten by subclasses.
After all the abstract methods, create a subclass object call. If the subclass only overwrites
This subclass is an abstract class.
Abstract classes are not very different from general classes.
How to describe a thing, but it appears
Something you can't understand.
These Uncertain parts are also the functions of this thing and need to be clearly displayed,
The subject cannot be defined.
It is represented by an abstract method.
Abstract analogy generally involves Abstract Functions (or methods ). That is, you can
Define abstract methods.
Abstract classes cannot be instantiated.
Special: abstract classes do not define abstract methods.
Create an object.
// Exercise
Assume that we need to model employees (data models) when developing a system.
The employee has three attributes: name, employee ID, and salary. The manager is also an employee.
In addition to employee attributes, there is also a bonus attribute. Use step
Design the employees and managers, and provide necessary methods in the class.
Row attribute access.
Define an employee class: including name Id pay
Define a manager class: inherits the employee class and has its own unique bouns.
Class employee {
Private string name;
Private string ID;
Private double pay;
Employee (string name, string ID, double pay ){
This. Name = Name;
This. ID = ID;
This. Pay = pay;
}
Public abstract void work ();
}
Class manger extends emplayee {
Private int bonus;
Manager (string name, string ID, double pay, its
Bonus ){
Super (name, ID, pay );
This. Bonus = bonus;
}
Public void work (){
Syso ("Manager work ");
}
}
Class pro extends employee {
Pro (string name, string ID, double pay ){
Super (name, ID, pay );
}
Public void work (){
Syso ("Pro work ");
}
}
Class test {
Public static void main (string [] ARGs ){
}
}
Interface
Format: interface {}
The member modifiers in the interface are fixed.
* Member constant: public static final
* Member function: public abstract
The emergence of interfaces reflects "Multi-inheritance" in another form, that is, "multi-entity
".
Interface: it is initially understood as a special abstract class.
When the methods in the abstract class are abstract, the class can be implemented through the interface
.
Class is used to define a class
Inferface is used to define interfaces.
Interface Definition, format features:
1. Common definitions in Interfaces: constants and abstract methods
2. All the members in the interface have fixed modifiers.
Constant modifier: public static final
Method modifier: public abstract
Conclusion: all the members in the interface are public.
** Classes and classes are inherited.
** The class and interface are implementation relationships.
** Interfaces and interfaces are inherited.
* ********* Only interfaces and interfaces can be inherited from each other.
To
Imprements
Interface: you cannot create objects because there are abstract methods. Quilt class required
Now, the subclass can be instantiated only after all the abstract methods in the interface are overwritten.
. Otherwise, the subclass is an abstract class.
An interface can be implemented by multiple classes and is not supported by multi-inheritance. Java
Multiple implementations are supported.
Interface features:
1. interfaces are rules for external leakage.
2. interfaces are program function extensions
3. interfaces can be used for multiple implementations.
4. There is an implementation relationship between the class and the interface, and the class can inherit
Class to implement multiple interfaces at the same time
5. interfaces and interfaces can have an inheritance relationship.
Polymorphism
Definition: multiple forms of existence of a certain type of things.
Polymorphism: it can be understood as multiple manifestations of the existence of things.
Learn polymorphism based on the following questions:
1. Forms of Polymorphism
The parent class is referenced only to its own subclass object.
The parent class can also receive its own subclass objects.
2. The premise of Polymorphism
It must be related to a class, inheritance, or implementation.
There is usually another premise: Overwrite
3. What are the benefits of polymorphism?
1. Improved Program scalability
4. How to Apply Polymorphism
5. disadvantages of Polymorphism
It improves scalability, but can only use the reference of the parent class to access members in the parent class.
6. the characteristics of the Code in the appearance of polymorphism (Precautions for using polymorphism)
* *********** Interview questions:
* ** In polymorphism, non-static member functions have the following features:
During the compilation period: see whether there are called methods in the class to which the referenced variable belongs,
If yes, the compilation succeeds. If no compilation fails.
In runtime: see whether methods are called in the class to which the object belongs.
The simple conclusion is: when a member function is called for multi-state, compile the function to the left and run it.
Right side.
In polymorphism, member variables have the following features:
Regardless of compilation and running, refer to the left side (class to which the referenced variable belongs ).
In polymorphism, static member functions have the following features:
For compilation and running, refer to the left.
Animal A = new CAT (); // type improvement, also called upward transformation.
Forcibly reference the parent class. Convert to subclass type. This is called downward transformation.
Do not convert a parent class object to a subclass type.
If the parent application points to its own subclass object, the application can be
Can also be forcibly converted.
Polymorphism is constantly changing from beginning to end.
Use the instanceof keyword to determine whether the reference type matches.
// Computer running instance
// The computer runs based on the motherboard
/* Too troublesome
Class mainboard {
Public void run (){
Syso ("mainboard Run ");
}
Public void usenetcart (netcard c ){
C. Open ();
C. Close ();
}
}
Class netcard {
Public void open (){
Syso ("netcar open ");
}
Public void close (){
Syso ("netcart close ");
}
}
Class test {
Public static void main (string [] ARGs ){
Mainboard MB = new mainboard ();
MB. Run ();
MB. usernetcard (New netcard ());
}
}
*/
// Use the interface
Interface PCI {
Public void opem ();
Public void close ();
}
Class netcard implements PCI
{
Public void open (){
Syso ("netcar open ");
}
Public void close (){
Syso ("netcart close ");
}
}
Class mainboard {
Public void run {
Syso ("mainboerd Run ");
}
Public void userpci (pCI p) // pci p = new netcard
() // Interface type reference points to its own subclass object.
{
If (P! = NULL ){
P. open ();
P. Close ();
}
}
}
Class test {
Public static void main (string [] ARGs ){
Mainboard MB = new mainboard ();
MB. Run ();
MB. usepci (null );
MB. usepci (New netcard ());
}
}
/* Database operation instance (pseudo code)
Data is user information
1. Connect to the database
JDBC hibernate
2. operate databases
C: Create R: Read U: Update D: Delete
3. Shut down the database
*/
Interfaceuserinfodao
{
Public void add (User user );
Public void Delete (User user );
}
Class dboperate {
Public static void main (string [] ARGs ){
// Userinfobyjdbc UI = new userinfobyjdbc ();
// Userinfobybyhibernate UI = new
// Userinfobybyhibernate ();
// If you do not want to modify the image, use reflection.
Userinfodao UI = new userinfobyjdbc ();
Ui. Add (User );
Ui. Delete (User );
}
}
Class userinfobyjdbc implements userinfodao {
Public void add (User user ){
/*
1. Connect to the database.
2. Use SQL to add statements to add data.
3. Close the connection.
*/
}
Public void Delete (User user ){
/*
1. Connect to the database
2. Use SQL to add a statement to delete data
3. Disable data
// You can use the database connection pool later.
*/
}
}
Class userinfobybyhibernate implements userinfodao {
Public void add (User user ){
/*
1. Connect to the database.
2. Use SQL to add statements to add data.
3. Close the connection.
*/
}
Public void Delete (User user ){
/*
1. Connect to the database
2. Use SQL to add a statement to delete data
3. Disable data
// You can use the database connection pool later.
*/
}
}
// Dao Full name: Data Access Object