Java beginner notes 12

Posted by delorian on Wed, 13 Oct 2021 04:09:44 +0200

1 final keyword

(1) Final Chinese means: final, final
(2) final can modify classes, attributes, methods and local variables. (cannot modify constructor)
(3) In some cases, programmers may use final if they have the following requirements:

  1. When you want the class not to be inherited, you can use final

  2. When you want a method of the parent class not to be overridden / overridden by a subclass, you can modify it with the final keyword

  3. When you want the value of an attribute of a class not to be modified, you can modify it with final

  4. When you want a local variable not to be modified, you can use the final modifier

2 details of final keyword

  1. The attribute modified by final is also called a constant, generally XX_ XX to name

  2. When defining the final modified attribute, it must be assigned an initial value and cannot be modified later. The assignment can be in one of the following positions [select a position to assign an initial value]:
    (1) When defining

    (2) In the constructor

    (3) In a code block

  3. If the final modified attribute is static, the initialization location can only be
    (1) When defining
    (2) In static code block
    Cannot assign value in constructor

  4. The final class cannot inherit, but it can instantiate objects

  5. If the class is not final but contains a final method, the method cannot be overridden but can be inherited

  6. Generally speaking, if a class is already final, there is no need to modify the method to final

  7. final cannot modify a constructor (that is, a constructor)

  8. Wrapper classes (Integer,Double,Float, Boolean, etc. are all final), and string is also final, so it cannot be inherited~

  9. final and static are often used together, which is more efficient and will not lead to class loading. The underlying compiler has done optimization.
    (1) When defining, the final static attribute is assigned a value, and the class will not be loaded
    (2) When defining, the final static attribute is not assigned a value, and the class is loaded

3 final exercises

4 abstract abstract class, abstract method

(1) Uncertainty of parent method
Consider designing this method as an abstract method
=>The so-called abstract method is a method without implementation
=>No implementation means that there is no method body
=>When there are abstract methods in a class, you need to declare the class as abstract
=>In general, abstract classes are inherited and their subclasses implement abstract methods
(2) When you use the abstract keyword to modify a class, the class is called an abstract class

Access modifier    abstract   Class name {...}

(3) When a method is modified with the abstract keyword, the method is an abstract method

Access modifier    abstract    Return type method name(parameter list) ;  //No method body

(4) The value of abstract classes lies more in design. After the designer has designed it, subclasses inherit and implement it

5 abstract abstract abstract classes, abstract method details

(1) Abstract classes cannot be instantiated
(2) Abstract classes do not have to contain abstract methods. That is, an abstract class can have no abstract method.
(3) Once a class contains an abstract method, the class must be declared as an abstract class
(4) abstract can only modify classes and methods, not attributes and other
(5) Abstract methods cannot be decorated with private, final and static, because these keywords are contrary to rewriting.
(6) Abstract classes can have any member (abstract class or class in essence), such as non abstract methods, constructors, static attributes, etc
(7) Abstract methods cannot have subjects, that is, they cannot be implemented
(8) If a class inherits an abstract class, it must implement all abstract methods of the abstract class unless it is also declared as an abstract class.

6 abstract abstract class, abstract method exercises

7. Can static methods be overridden?

java static methods cannot be overridden

Rewriting of java beginner notes 7

8 formwork design mode

Parent class



main function

Quick start case of 9 interface

  1. USB interface
package demo.interface_;

 * @Package: demo.interface_
 * @ClassName: UsbInterface
 * @Author: ASUS
 * @CreateTime: 2021/10/12 20:27
 * @Description:  USB Interface
public interface UsbInterface {
    //Specify relevant methods of interface
    public void start();
    public void stop();


  1. mobile phone
package demo.interface_;

 * @Package: demo.interface_
 * @ClassName: phone
 * @Author: ASUS
 * @CreateTime: 2021/10/12 20:28
 * @Description:  Mobile phone, access USB interface
 * phone Class needs to implement the methods declared by the UsbInterface interface
public class phone implements UsbInterface{
    public void start() {
        System.out.println("The phone starts working...");

    public void stop() {
        System.out.println("The phone stops working...");

  1. camera
package demo.interface_;

 * @Package: demo.interface_
 * @ClassName: Camera
 * @Author: ASUS
 * @CreateTime: 2021/10/12 20:29
 * @Description: Camera class, access USB interface
 * Camera Class needs to implement the methods declared by the UsbInterface interface
public class Camera implements UsbInterface{
    public void start() {
        System.out.println("Camera preparation...");

    public void stop() {
        System.out.println("The camera stops working...");

  1. computer
package demo.interface_;

 * @Package: demo.interface_
 * @ClassName: Computer
 * @Author: ASUS
 * @CreateTime: 2021/10/12 20:31
 * @Description:  Manage USB interface
public class Computer {
    public void work(UsbInterface usbInterface){


  1. test
package demo.interface_;

 * @Package: demo.interface_
 * @ClassName: InterfaceExample01
 * @Author: ASUS
 * @CreateTime: 2021/10/12 20:26
 * @Description:  Test end
public class InterfaceExample01 {
    public static void main(String[] args) {
        //Create mobile phone class and camera class objects
        Camera camera = new Camera();
        phone phone = new phone();
        //Create computer class
        Computer computer = new Computer();
        //Connect your mobile phone to your computer;
        //Connect the camera to the computer;


10 interface

(1) Popular understanding: it is to give some unimplemented methods, package them together, and write them out according to the specific situation when a class is to be used.
(2) Syntax:

interface Interface name{

1. Abstract method. The abstract keyword can be omitted
2. Write the implementation method by default and add the keyword default
3. Static method with static keyword

class Class name implements Interface{
Own attribute;
Own method;
The abstract method of the interface that must be implemented

(3) Before Jdk7.0, all methods in the interface had no method body, that is, they were abstract methods. After Jdk8.0, the interface can have static methods and default methods, that is, there can be specific implementations of methods in the interface.

11 interface details

(1) Interface cannot be instantiated
(2) All methods in the interface are public methods. Abstract methods in the interface can be modified without abstract, so the keywords public and abstract can be omitted
(3) If a common class implements an interface, it must implement all the methods of the interface, which can be solved by using the alt+enter shortcut key
(4) When an abstract class implements an interface, it may not implement the abstract method of the interface
(5) A class can implement multiple interfaces at the same time
(6) The attributes in the interface can only be final and are public static final modifiers.
For example: int a=1; In fact, public static final int a=1; (must be initialized)
(7) Access form of attribute in interface: interface name. Attribute name
(8) Interfaces cannot inherit other classes, but they can inherit multiple individual interfaces
For example: interface A extends B,C
(9) Interface modifiers can only be public and default, which is the same as class modifiers.
(10) The subclass inherits the parent class and implements an interface. If both the parent class and the interface have an attribute with the same name, it is necessary to use super to indicate the parent class and interface name. Attribute name to indicate the interface when calling.
For example:

Interface exercises:

12 inherit VS interface

When a subclass inherits the parent class, it automatically has the function of the parent class; If subclasses need to extend functions, they can be extended by implementing interfaces. Popular understanding: implementing interfaces is a supplement to the java single inheritance mechanism.

  1. The interface and inheritance solve different problems
    The value of inheritance mainly lies in: solving the reusability and maintainability of code.
    The value of interface mainly lies in: design, design various specifications (Methods) and let other classes implement these methods. That is, the interface is more flexible.
  2. The interface is more flexible than inheritance
    Interfaces are more flexible than inheritance. Inheritance satisfies the is - a relationship, while interfaces only need to satisfy the like - a relationship
  3. The interface realizes code decoupling to a certain extent [i.e. interface normalization + dynamic binding]

13 interface polymorphism

14 interface polymorphism transmission

Topics: Java