Java Review Summary 2

Source: Internet
Author: User
Tags modifier modifiers

Static methods
As with static variables, we can also use the static modification method, called a static method or a class method.
In fact, the main method we've been writing about is static methods.
A static method can directly invoke a static member in the same class, but not a non-static member directly.
If you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.
In the normal member method, the same non-static variable and static variable can be accessed directly.
A non-static method cannot be called directly in a static method, and a non-static method needs to be accessed through an object.
Static initialization blocks
In Java, data can be assigned by initializing blocks.
In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class. If you use
The static modifier initializes the block, which is called the statically initialized block.
Note: Static initialization blocks are executed only once when the class is loaded, and static initialization blocks can only assign values to static variables and cannot initialize normal member variables.
Object-oriented features
Object-oriented three main features: encapsulation, inheritance, polymorphism
Encapsulation: Hides some information about a class inside a class and does not allow direct access by external programs.
Instead, the method provided by the class implements the operation and access to the hidden information.
Pros: 1 access to Data 2 The instance details of the hidden class can be easily modified and implemented by a prescribed method


The role of packages in Java: Managing Java files To resolve file conflicts with the same name
Use of packages: You can use classes from other files in one file by using the Import keyword.
In Java, the naming conventions for packages are all lowercase letters spelled
Access modifiers in Java
Access modifiers: You can modify the access scope of properties and methods
Access Modifiers Summary
Access modifier into similar to the other sub-class
Private is
The default is Yes
Protected Yes Yes Yes
Public Yes Yes Yes Yes
The This keyword in Java
The This keyword represents the current object
This. Property operation properties of the current object
This method invokes the current object.
The This keyword is often used when encapsulating the properties of an object
Inner classes in Java
An inner class (Inner Class) is a class that is defined in another class. Corresponding to this, the class containing the inner class is called the outer class.
The main functions of the inner class are as follows:
1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class
2. The method of the inner class can access all the data of the external class directly, including the private data
3. Functions implemented by internal classes can also be implemented using external classes, but sometimes it is easier to use internal classes
The inner classes can be divided into the following types:
member Inner class
Static Inner class
Method Inner Class
Anonymous inner class
Static inner classes in Java
Static inner classes are internal classes of static adornments, which are characterized by:


1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external class (). Members


2. If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name


3. When creating an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner class ();
Method inner Classes in Java
The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.
Note: Because the method inner class cannot be used outside the methods of the outer class, the method inner class cannot use the access control and the static modifier.
Inner classes cannot have their own member methods and member variables

Inherited
Inheritance is a relationship between classes and classes, and is a relationship of "is a"
Inheritance in Java is single-inheritance
Benefits of Inheritance: Subclasses have all the properties and methods of the parent class (cannot use private adornments)
Implementing code Reuse
Syntax rules:
Class Subclass extends Parent class
Override of Method
If the subclass is not satisfied with the method that inherits the parent class, it is possible to override the method inherited by the parent class.
A method that invokes a subclass in preference when calling a method
Syntax rules:
return value type
Method name
Parameter type and number
Is the same as the method inherited by the parent class, which is called the override of the method
Initialization Order of inheritance
1 initializing the parent class in the initial subclass
2 initializes the properties in the initialization object, in the execution of the constructor method
Final keyword
Use final keyword to identify "final" meaning
Final can modify classes, methods, properties, and variables
Final decorated class, the class is not allowed to be inherited
Final decoration method, the method is not allowed to be overwritten (overridden)
Final modifier Properties
The property of the class is not implicitly initialized (the class's initialization property must have a value)
or assign a value in the construction method (but only one of them is selected)
Final modified variable, the value of the variable can be assigned only once, which becomes a constant
Super keyword
Used inside an object, you can represent the parent class object
Application: The constructor of its parent class must be called during the construction of a subclass.
If the constructor method of the calling parent class is not displayed in the child class's constructor,
The default method is to call the parent class without a parameter constructor.
If the calling constructor method is displayed, the first line of the child class must be constructed in the method.
If the constructor method that calls the parent class is not displayed in the subclass construction method, and the parent class does not have a parameterless constructor, the compilation error occurs.
Object class
The object class is the parent class of all classes, and if a class is not explicitly identified with the extends keyword to inherit another class, the class defaults to inheriting
Object class
Method in the object class, suitable for all subclasses.
1 toString () method
The hash code (object address string) of the object returned when defining the ToString () method inside the object class
You can represent the properties of an object by overriding the ToString () method
2 equal () method
Compares whether a reference to an object points to the same memory address

In general, compare two objects when comparing his values to be consistent, so rewrite

Polymorphic
Multiple forms of an object
1 referencing polymorphism
References to the parent class can point to objects of this class
A reference to the parent class can point to the object of the child class
2 Method polymorphism
When you create this type of object, the method that is called is the method of this class
When you create a subclass object, the method that is called is either a method that is overridden by a subclass or an inherited method
Conversion of reference types in polymorphic
1 Upward type conversion (implicit/automatic type conversion), is small type to large type conversion
2 downward type conversion (forced type conversion), is large type to small type
3 instanceof operator To resolve types of referenced objects and avoid security issues with type conversions
Abstract classes in Java
Syntax definition: The abstract keyword is used to decorate the class before it is abstract.
Application Scenarios:
1 in some cases, a parent class just knows what kind of method its subclasses should contain
, but it is not possible to know exactly how these subclasses implement these methods.
2 Abstracts an abstract class from multiple classes with the same characteristics, using this abstract class as a template for subclasses,
Thus avoiding the arbitrariness of sub-class design.
Role: Restriction subclasses must implement certain methods, but do not pay attention to implementation details
Usage rules
1 abstract class definition
2 abstract defining abstractions, declarations only, no implementation required
3 classes that contain abstract methods are abstract classes
4 Abstract classes can contain ordinary methods, or there can be no abstract method
5 Abstract classes cannot be created directly, you can define reference variables
Interface in Java
Interface Concepts
Interfaces can be understood as a special class, consisting of global constants and common abstract methods.
A class is a concrete implementation body, and an interface defines a specification that a batch of classes must follow, and the interface does not care
These classes have internal data and do not care that some methods must be provided in these classes.
Interfaces are used to be inherited, implemented, modifiers are generally recommended to use public,
Note: You cannot use private and protected to decorate interfaces
Constant
The properties in the interface are constants, and even if the public static final modifier is not added to the definition, the system automatically adds
Method:
The method in the interface can only be an abstract method, always used, even if the definition does not add the public abstract modifier, the system will automatically add
Using interfaces:
A class can implement one or more interfaces that implement an interface using the Implements keyword. A class in Java can inherit only one parent class.
is inflexible and can be supplemented by implementing multiple interfaces.
If you want to inherit the parent class, the inherited parent class must precede the implementation of the interface
Using interfaces:
Interfaces are often used in conjunction with anonymous internal classes.


Anonymous inner classes are internal classes that do not have names, and are used to focus on implementations rather than on the name of the implementation class

Uml
Concept
Unified Modeling Language (UML)
Also known as Unified Modeling Language or standard modeling language
is a graphical language that supports modelling and software system development
Provides modeling and visualization support for all phases of software development
Common UML diagrams
Use case diagram
Use case diagrams can visualize how systems meet the business rules they collect, and
Specific user needs and other information.
Sequence diagram
Sequence diagrams are used to display these interactions between objects in a series of sequences that occur interactively.
Class diagram
UML class diagrams, business logic, and all supporting structures are used together to define the entire code structure.

Java Review Summary 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.