Static (implement attribute method sharing)
Abstract (a class may inherit multiple parent classes)
Inner class (there are classes inside the class)
Static properties
Static attributes belong to a class, which can be accessed directly by class name or by object
Non static attributes belong to objects. You must use objects to access them. There is no other way!
Static attributes belong to a class and are shared by all objects of this class
Storage location of static attributes:
The static properties in the class are stored in the method area in memory along with the class.
When creating an object, only the information of non static attributes defined in the class will be saved in the object, and static attributes will not enter the object.
Initialization of static properties:
Both static and non static attributes can only be used after initialization, or the system assigns default values to attribute initialization,
Either we assign the attribute manually.
Property initialization time:
Non static attributes: after creating an object, the system will automatically initialize the non static attributes in the object and assign default values. For this reason, non static attributes can only use object methods after creating an object
Static attributes: when a class is loaded into memory (method area), the system initializes and assigns default values to the static attributes in the class. Therefore, even if the object has not been created, as long as the class is loaded into memory, you can directly use the class name to access the static attributes, because the static attributes have completed the operation of initializing and assigning default values at this time.
Static methods can be inherited, but cannot be overridden (but methods with this name can be written (name parameters can be the same))
this cannot be accessed in a static method
Static code block, also known as static initialization code block, is used to initialize static attributes in a class
Execution time of static code block:
Because the static code block has no name, we can't call it actively. It will be executed automatically when the class is loaded.
Therefore, the static code block can initialize and assign values to the static attributes in the class earlier.
Moreover, the static code block will be automatically executed only once, because the JVM will only load a class once in a run!
public class Demo { public static int num; static{ num = 10; } } public static void main(String[] args){ System.out.println(Demo.num);//The output is 10 }
Anonymous code block:
Similar to static code blocks, there is also a non static code block, also known as anonymous code block, which is used to initialize non static attributes
Execution time of anonymous code block: because the anonymous code block has no name, we can't call it actively. It will be executed automatically when the object is created and before the constructor is executed. And anonymous code blocks are automatically executed before each object is created.
public class Demo { public int num; { num = 10; } } public static void main(String[] args){ Demo demo = new Demo(); System.out.println(demo.num);//The output is 10 }
public class Demo { static { System.out.println("Static code block execution"); } { System.out.println("Anonymous code block execution"); } public Demo(){ System.out.println("Constructor execution"); } } public static void main(String[] args){ new Demo(); new Demo(); } //The output result is: Static code block execution Anonymous code block execution Constructor execution
The process of creating and initializing objects:
Student s = new Student();
Take this code as an example:
Load the Student class, initialize the static properties in the class, assign default values, and allocate memory space to the static methods
Executing static blocks of code in a class
Allocate the memory space of the object in the heap, and initialize the non static attributes in the object to assign default values
Call Student's parent constructor
Display and assign values to the attributes in Student, for example, public int age = 20;
Execute anonymous code block
Execute constructor code
=Assign the memory address of the object to the variable s
public class Person{ private String name = "zs"; public Person() { System.out.println("Person constructor "); print(); } public void print(){ System.out.println("Person print method: name = "+name); } } class Student extends Person{ private String name = "tom"; { System.out.println("Student Anonymous code block"); } static{ System.out.println("Student Static code block"); } public Student(){ System.out.println("Student constructor "); } public void print(){ System.out.println("student print method: name = "+name); } public static void main(String[] args) { new Student(); } }
result
Student static code block
Person constructor
student print method: name = null
Student anonymous code block
Student constructor
Note 1: subclasses override the methods of the parent class. In the process of creating subclass objects, the overridden methods in subclasses must be called by default
Note 2 that the display assignment of non static attributes is after the execution of the parent constructor and before the execution of anonymous code blocks in subclasses
Note 3, in the above code, because the rewrite of the method calls the rewritten print method in the subclass, and this method happens to be invoked in the execution of the parent class constructor. At this time, the name attribute in the subclass has not been displayed and assigned, so the output result is null
Note 4 that if the value of name is also output in the anonymous code block of the subclass, tom will be displayed because the display assignment of the attribute has been completed
Static import
import . . . . .
final
A class decorated with final cannot be inherited, that is, this class has no subclasses.
Methods decorated with final can be inherited by subclasses, but cannot be overridden by subclasses.
The variable modified with final becomes a constant, and it can only be assigned a value once, and an error will be reported for the second assignment
The final variable declared with static will not be initialized by default, so it must be assigned by itself, otherwise an error will be reported (it can be assigned in the static code block, and non-static can be assigned in the anonymous block) (static will assign an initial value after default initialization, which will make the subsequent assignment meaningless)
Constant names are generally all uppercase
abstract
abstract
abstract modifier, which can modify classes and methods
If Abstract modifies a method, the method is an abstract method.
Characteristics of abstract methods:
Only the declaration of methods, no implementation of methods
If Abstract modifies a class, it is an abstract class.
Differences between abstract and non abstract classes:
The abstract class uses the abstract modifier, but the non abstract class does not
Abstract methods can be written in abstract classes, but not in non abstract classes
Abstract classes cannot be instantiated to create objects, while non abstract classes can be instantiated to create objects
Abstract classes are used to be inherited by subclasses. Subclasses inherit abstract classes and implement abstract methods in abstract classes.
Therefore, when we encounter an abstract class, the class must be inherited, and then implement the abstract methods or rewrite the ordinary methods.
Note that implementing the abstract method in the parent class is different from overriding the ordinary method in the parent class, but their syntax requirements and operation methods are exactly the same. You can directly treat the implementation of the abstract method as a rewriting method.
If the abstract class does not instantiate the creation object, is there a constructor in the abstract class?
There is a constructor, which is called by subclasses. Subclasses inherit the parent class. When subclasses create objects, they will call the constructor of the parent class first
The subclass inherits the abstract parent class. The subclass can choose to implement all the abstract methods in the parent class. If any abstract method is not implemented by the subclass, the subclass should also declare itself as an abstract class, so the subclass can only wait. Another subclass can inherit itself to implement the remaining abstract methods that are not implemented, Until all abstract methods are implemented.
Abstract classes can have no abstract methods (constructors), but the classes that define abstract methods must be abstract classes
interface (defined to be implemented)
Reference data type: class, array, interface
Interface is another reference data type besides class and array
Unlike classes, interfaces encapsulate member variables, constructor methods and member methods, while interfaces encapsulate methods and static constants.
The definition of an interface is similar to that of a class, but the interface needs to be defined using the interface keyword
The interface will eventually be compiled into Class file, but it must be clear that the interface is not a class, but another reference data type
//Use the interface keyword to define an interface public interface Action {//Special parent class //Static constants in interfaces public static final String OPS_MODE = "auto"; //Abstract methods in interfaces public void start(); //Abstract method of interface public void stop(); }
Note 1: the keyword class is used for defining classes and the keyword interface is used for defining interfaces
Note 2: the attributes in the interface are public static constants, and the names of constants generally need to be capitalized
Note 3 that the methods in the interface are abstract methods
Note: in JDK8, it is also allowed to write static methods and default methods in the interface
Note: JDK9 also allows you to write private methods in interfaces
Note 4 that the abstract modifier is not required for the abstract methods in the interface, because the methods in the interface are abstract methods by default
The attributes in the interface are modified by public static final (constant) by default
The methods in the interface are modified by public abstract by default
So these can be omitted
There is no constructor in the interface
Interface implementation:
The relationship between classes is inheritance, and the relationship between classes and interfaces is implementation: a class implements one or more interfaces (special inheritance)
Unlike classes, interfaces can be instantiated to create objects, while interfaces cannot create objects and can only be implemented by other classes.
If a class implements an interface, it can be called the implementation class of this interface. The effect of class implementing an interface is similar to that of class inheriting a parent class. The format is similar, but the keywords are different. The implementation uses the implements keyword, and the inheritance uses the extensions key
Word.
If a class implements an interface, it must implement all abstract methods in the interface, otherwise the class itself must be declared as an abstract class.
public interface Action { void run(); void sayHello();//Ordinary methods in interfaces are abstract methods } //Class implements an interface and does not implement the abstract methods in the interface, then this class must be declared as an abstract class abstract class Student1 implements Action{ } //Class implements the interface and implements all the abstract methods in the interface class Student2 implements Action{ public void run(){ //... } public void sayHello(){ //... } }
A class can implement multiple interfaces. If it implements multiple interfaces, it needs to implement all the abstract ideas in these interfaces
public interface Action { void run(); void sayHello(); } public interface Mark{ void star(); } public interface Action { void run(); void sayHello(); } public interface Mark{ void star(); }
So java is called single inheritance multiple implementation
Interfaces can also be inherited, and multiple interfaces can be inherited
//The class that implements the interface will have the function of run public interface Runable { void run(); } //The class that implements the interface will have the function of fly interface Flyable{ void fly(); } //The class implementing the interface will have the function of run, fly and doSomething unique to the Action interface interface Action extends Runable,Flyable{ void doSomething(); } //To implement class and Action interface, you must implement all abstract methods in Action and its parent interface class Demo implements Action{ @Override public void run() { } @Override public void fly() { } @Override public void doSomething() { } }
How can developers write code according to our pre-designed method?
Interface oriented programming:
access control
Modifiers: public > protected > Default > private
Single example (design mode):
Whenever you get an object through this class, you will always get the same object
class A{ //Create A private static A instance= new A(); //public A(){} private A(){} //Provides a method to get the created object public static A tt(){ return instance; } } class B{ } public class Main { public static void main(String[] args) { A a1 = A.tt(); A a2 = A.tt(); B b1 = new B(); B b2 = new B(); System.out.println(a1.equals(a2)); System.out.println(b1.equals(b2)); } } true false