Home >Java >javaTutorial >Summary of Java features
1. Inheritance (Keyword: extends): (child parent class)
1. Improve the reusability of code.
2. Create a relationship between classes. Only with this relationship can we have the characteristics of polymorphism.
1.1 Variable
If a non-private (private) member variable with the same name appears in the child parent class, the child class needs to access the variable in this class, use this, and the child class needs to access the same name in the parent class For variables, use super.
The use of super is almost the same as the use of this.
This represents a reference to an object of this class.
The reference of the parent class object represented by super.
1.2 Functions in the subclass and parent class
When a function in the subclass appears that is exactly the same as the parent class,
When the subclass object calls the function, the content of the subclass function will be run. Just like the function of the parent class is overridden.
This situation is another characteristic of functions: rewriting (overwriting)
When a subclass inherits the parent class, it inherits the functions of the parent class into the subclass, but although the subclass It has this function, but the content of the function is inconsistent with the parent class. In this case, there is no need to define a new function. Instead, use the override feature to retain the function definition of the parent class and rewrite the function content.
Notes on overwriting:
1. When a subclass overwrites a parent class, it must be ensured that the permissions of the subclass are greater than or equal to the permissions of the parent class before it can be overwritten, otherwise the compilation will fail. (If you do not write the permission modifier, the default permission is between private and public.)
2. Static can only override static.
Overloading and rewriting:
Overloading: only look at the parameter list of the function with the same name.
Rewriting: The method name and return type in the child and parent classes must be exactly the same. (That is: the return type and method name () must be the same)
1.3 Constructor in subclasses.
When initializing a subclass object, the constructor of the parent class will also run. That is because the constructor of the subclass has an implicit statement super() in the first line by default;
Why Subclasses must access the constructor in the parent class.
Since the data subclasses in the parent class can be obtained directly, when all subclass objects are created, you need to first check how the parent class initializes the data. Therefore, when initializing the object, the subclass must first access the constructor in the parent class. If you want to access the constructor specified in the parent class, you can specify it by manually defining the super statement.
Note: The super statement must be defined in the first line of the subclass constructor.
Conclusion:
All constructors of subclasses will access the constructor with empty parameters in the parent class by default.
Because the first line of each constructor in a subclass will have an implicit super();
When there is no constructor with empty parameters in the parent class, the subclass must manually specify the requirements in the form of a super statement. Access the constructor in the parent class.
Of course: the first line of the subclass's constructor can also manually specify this statement to access the constructor in this class. At least one constructor in the subclass will access the constructor in the parent class. (Because both this() and super() can only be placed in the first line of the constructor, only one of them can exist in the constructor.)
Note: Never try to get the other The functions of the class are inherited to simplify the code. There must be a relationship between classes before they can be inherited. The affiliation is a .
2.final: final. as a modifier.
2.1 Classes, functions, and variables can be modified.
2.2 Classes modified by final cannot be inherited. In order to avoid being inherited, avoid overriding functions by subclasses.
2.3 Methods modified by final cannot be overridden
2.4 A variable modified by final is a constant that can only be assigned a value once. It can modify either member variables or local variables. When describing things, when some data appears, the values are fixed. In order to enhance readability, these values are given names. Easy to read. This value does not need to be changed, so the final modification is added. As a constant: The writing convention for constants is all capital letters if they consist of multiple words. Words are connected by _. (eg: final double PI = 3.14; final int SET_ON = 1;)
2.5 When an internal class is defined at a local location in the class, it can only access the local variables modified by final.
3. Abstraction:
3.1 Abstract methods must be in abstract classes.
3.2 Both abstract methods and abstract classes must be modified by the abstract keyword. (abstract class class name { abstract void method name (); })
3.3 Abstract classes cannot use new to create objects. Because there is no point in calling abstract methods.
3.4 To use the abstract methods in an abstract class, all abstract methods must be rewritten by the subclass and then the subclass object can be called. If a subclass only covers some abstract methods, then the subclass is still an abstract class.
Similarities and differences between general classes and abstract classes: There is not much difference between abstract classes and general classes. You can describe things how you want to describe them, but there are some incomprehensible things about them. These indeterminate parts, which are also the function of the thing, need to appear explicitly. But the subject cannot be defined. Represented through abstract methods.
Abstract classes have one more abstract function than ordinary classes. That is, abstract methods can be defined in classes.
Abstract classes cannot be instance methods.
Special: Abstract methods do not need to be defined in abstract classes. This only prevents the class from creating objects.
4. Template design pattern:
When defining a function, part of the function is certain, but part of it is uncertain, and the certain part uses the uncertain part, then the uncertain part will be used. Partially exposed. This is done by subclasses of this class.
5.Interface:
Initial understanding can be considered as a special abstract class. When the methods in an abstract class are abstract, then the class can be represented in the form of an interface.
class: used to define classes
interface: used to define interfaces. An interface is also a class. Class files can be generated.
When defining an interface, the format characteristics are:
1. Common definitions in interfaces: constants, abstract methods. The emergence of interfaces embodies "multiple inheritance" in another form, that is, "multiple implementations".
2. Members in the interface have fixed modifiers.
Constant: public static final
Method: public abstract
Remember: all members in the interface are public.
Interface: It is not possible to create objects because there are abstract methods. It needs to be implemented by a subclass. Only after the subclass rewrites all the abstract methods in the interface can the subclass be instantiated. Otherwise the subclass is an abstract class.
eg: interface Inter
{
//It’s okay if the public static final and public abstract modifiers are missing, because members in the interface have fixed modifiers. But it is best to write during development to enhance readability.
public static final int SET_ON = 1;
public abstract void show();
}
//Use interface implementations
class Test implements Inter
{
public void show (){} //You need to override the abstract method show to create an object
}
class InterfaceDemo
{
public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.SET_ON);
}
}
Features of the interface:
The interface is Rules for external exposure.
The interface is the functional extension of the program.
Interfaces can be used for multiple implementations.
There is an implementation relationship between classes and interfaces, and a class can inherit one class and implement multiple interfaces at the same time.
There can be an inheritance relationship between interfaces.
6. Polymorphism:
Definition: multiple forms of existence of a certain type of thing.
eg: cats and dogs among animals
The corresponding type of the cat object is the cat type
Cat for animals.
Animal y = new cat();
Animal is the parent type extracted from the specific things of cats and dogs.
The parent type reference points to the subtype object.
6.1. Reflection of polymorphism:
The reference of the parent class points to its own subclass object.
The reference of the parent class can also receive its own subclass object.
eg: Parent class Parent class reference name = new subclass();//Type promotion. Upward transformation. To use the unique functions of a subclass, you need to transform downward: subclass new subclass reference name = (subclass) original parent class reference name
Keywords: instanceof Determine reference type
eg: if (a instanceof Cat) //Determine whether a is a cat type.
6.2. Prerequisites for polymorphism:
There must be a relationship between classes. Either inherit or implement.
Usually there is also a prerequisite: subclasses must override (rewrite).
The emergence of polymorphism has greatly improved the scalability of programs.
6.4. Disadvantages of polymorphism:
Improves scalability, but you can only use references from the parent class to access members of the parent class.
6.5 Characteristics of non-static member functions in polymorphism:
At compile time: See whether there is a calling method in the class to which the reference variable belongs. If there is, the compilation passes; if not, the compilation fails.
At runtime: See whether there is a method to call in the class to which the object belongs.
A simple summary is: when a member function is called polymorphically, look at the left side when compiling and the right side when running.
6.6 Characteristics of member variables in polymorphism:
Whether compiling or running, refer to the left side (the class to which the reference variable belongs.)
Characteristics of static member functions in polymorphism:
Regardless For compilation and running, refer to the left side (static itself can be called directly).
7.object: It is the direct or indirect parent class of all objects, the legendary God.
What is defined in this class must be the functions that all objects have.
Access rules for inner classes:
1. Inner classes can directly access members in outer classes, including private ones.
2. To access an inner class, an outer class must create an inner class object.
The above is the detailed content of Summary of Java features. For more information, please follow other related articles on the PHP Chinese website!