Interface, internal class

1. Interface

Interface is a reference data type, the keyword interface. A class can only inherit one class using extends, and implement multiple interfaces using implements. Inheritance first, implementation later

1. Features of the interface: [1] Interfaces can declare attributes, and attributes are of static final type.

        [2] The methods in the interface are all modified by public abstract.

        [3] The interface cannot be instantiated. The constructor cannot be defined in the interface.

        [4] An interface can inherit one or more other interfaces, using the extends keyword.

        [5] A class that implements the A interface must implement all the abstract methods in the A interface. Among them, this class is called the implementation class of the A interface, and the keyword implement is used.

        [6] The interface stipulates the the necessary ability to implement a class, and it also expands the class’s capabilities => The implementation class has the capabilities defined in the interface. => The implementation class has a interface

    Note: If the abstract method in the abstract class has the same name as the abstract method in the interface, the implementation class implements the abstract method in the abstract class, and the actual development process should be avoided as much as possible.

2. Abstract classes and interfaces

The same point: abstract classes and interfaces are reference data types, both contain abstract methods, and can be overridden abstract Methods, all can be polymorphic, all are transitive, and none of them can create objects.

Differences: 1. Only global static constants can be defined in the interface, not variables. Constants and variables can be defined in abstract classes.

   2, all methods in the interface are abstract methods. There can be zero, one, multiple, or all abstract methods in an abstract class (there can be non-abstract methods).

    3. The construction method cannot be defined in the interface. Abstract classes can have construction methods, but they cannot be used for instantiation. When subclasses are instantiated, the initialization operation belonging to the abstract class is completed.

    4. A class can only inherit one abstract class, but it can inherit multiple interface.

    5. The interface does not require the implementation class and the interface to be conceptually consistent. There is a “has-a relationship” between them. The abstract class and the subclass are conceptually the same. They There must be an “is-a” relationship between them

    6. Abstract classes and subclasses solve problems within modules (code reuse, rewriting, polymorphism), and interfaces solve problems between modules Problem => High cohesion, low coupling.

Three, Object

Common methods of java.lang.Object class: 1. Public boolean equals(Object) Comparing content

2, public native int hashCode () hash code

3, public java.lang.String toString () into the string

4, public final native java.lang. Class getClass() Get class structure information

              5、protected void finalize() throws java.lang.Throwable Throwable                          , protected void finalize() throws java.lang. java.lang.CloneNotSupportedException Clone

              7, public final void wait() throws java.lang.InterruptedException                      7, public final void wait() throws java.lang. Medium wake-up function

              9, public final native void notifyAll() The function of waking up all waiting threads in multi-threading

  Object is the direct or indirect parent class of all classes. For the root class. If a class does not explicitly inherit a class, it must inherit the Object class.

  toString() is used to output the string form of the object. The toString method of the default Object outputs the object type @memory address. Generally subcategories need to rewrite toString( ).

Four. ==and euqals

==Used for basic data type values ​​or whether the addresses of two objects are equal, two objects If they are equal (==), they must be the same object. Two objects without parent-child relationship cannot be compared.

equals() is used to determine whether the specified contents of two objects are equal:

a) The system class generally has covered equals(), and the comparison is the content.

  b) If the user-defined class does not override equals(), the equals(Object) of the parent class will be called, and the comparison of the equals of Object is the address

  c) User-defined To define a class, you need to rewrite equals() of the parent class

Note: Object == and equals compare addresses and have the same effect

Five. Inner class

< p>A class can be defined inside another class. This class is called an inner class, and the other class is called an outer class. The inner class exists as a member of the outer class

Necessary modifiers can be added before the inner class, but public is not available. Generally speaking, the default modifiers are used. Compiled style: Outer$Inner.class

Internal classes can be divided into member internal classes, static internal classes, method internal classes, anonymous internal classes according to modifiers and defined positions.

  1. Member inner class: public class Outer{class Inner{public void a(){}}}

    Outer outer= newOuter();  Inner inner = outer.new Inner();  inner.a();

   Inner class characteristics: [1] The inner class can directly access the private members of the outer class.

         [2] The inner class and the outer class define private attributes with the same name. Take name as an example. Avoid actual development as much as possible.

     ①Access the private members of the inner class: write name or this.name in the inner class

     ②Access the private members of the outer class: Outer.this.name

< p>  2, static inner class: public class Outer{static class Inner{public void a(){}}}

    InnerInner=new Outer.Inner();  inner.a();

p>

   Static inner class characteristics: [1] The static inner class can directly access the static members of the outer class.

         [2] The internal class and the external class have defined static properties with the same name. Take name as an example. Avoid actual development as much as possible.

     ①Access the private members of the inner class: write name or Inner.name in the inner class

     ②Access the private members of the outer class: Outer.name

3. Method inner class: public class Outer{public void a(){class Inner{m method}; new Inner().m();}} If the inner class object is used only once, use anonymous object

    Method inner class characteristics: The method inner class can directly access the local variables of the method. You cannot modify the local variables of the outer class method in the method inner class

   4. Anonymous inner class: An anonymous inner class must have an implementation (interface), take the interface Myinterface as an example

    public class Outer{public void a(){new MyInterface(){public void showInfo() {}}.showInfo();}}

    new Anonymous class means that an anonymous object is constructed and the interface is called through the anonymous object The method defined in.

6. Java garbage collection mechanism

1. The traditional C/C++ language requires the programmer to be responsible for recycling the allocated memory. Disadvantages of explicit garbage collection (Java can effectively avoid it):

  ①The program forgets to recycle in time, which leads to memory leaks and reduces system performance.

  ②The program error reclaims the memory of the program’s core class library, causing the system to crash.

2. Java automatically reclaims memory that is no longer used in the background by JRE, which is called garbage collection mechanism, GC.

   In addition to the above advantages, Java also has: ① It can improve programming efficiency.

              ② Protect the integrity of the program.

              ③ Its overhead affects performance. The JVM must keep track of the useful objects in the program and determine which ones are useless.

3. Features of the garbage collection mechanism gc:

1) The garbage collection mechanism reclaims the object space in the JVM heap memory, and is not responsible for reclaiming the stack memory data.

2) It is incapable of other physical connections, such as database connection, input stream output stream, and Socket connection.

3) The occurrence of garbage collection is unpredictable, and the program cannot precisely control the execution of the garbage collection mechanism.

4) The reference variable of the object can be set to null, which implies that the garbage collection mechanism can reclaim the object.

5) The current JVM has a variety of garbage collection implementation algorithms with different performances.

6) Before the garbage collection mechanism reclaims any object, it will always call its finalize method (if this method is overridden and a new reference variable re-references the object, the object will be reactivated).

7) Programmers can use System.gc() or Runtime.getRuntime().gc() to notify the system for garbage collection. There will be some effects, but it is still uncertain whether the system performs garbage collection.

8) Never actively call the finalize method of an object, it should be called by the garbage collection mechanism.

Leave a Comment

Your email address will not be published.