Java basics 10 object-oriented, Java 10 object-oriented
Abstraction
Concept: When a class does not have enough information to describe a specific thing in real life, it is an abstract class.
Abstract classes are generally descriptions of concepts.
Syntax:
[Access modifier] abstract class name {}
For example:
public abstract class Person{}
Features:
1. the abstract class cannot create objects, but it can have constructor. The constructor of the abstract class is called for the subclass. For example:
Public abstract class Person {
// This method is the correct public Person () {System. out. println ("constructor");} // but the object Person per = new Person () cannot be created like this ();
}
2. abstract classes can contain abstract methods, but static methods cannot be abstract.
Definition Syntax of abstract methods:
[Access modifier] Name of the abstract return value type method (parameter list );
Public abstract class Person {// abstract method public abstract void sleep (); // static method public static void say () {System. ou. println ("hello ");}}
If a class has an abstract method, the class must be an abstract class, but an abstract class does not necessarily have an abstract method.
If the subclass inherits the abstract class, the subclass must implement all the abstract methods in the parent class. Otherwise, the subclass must be declared as an abstract class.
Interface
An interface is used to declare a function (method). In an interface, there are only methods and constants.
Definition Syntax:
[Access modifier] interface name {}
Interface features:
The interface does not have a constructor, nor can it create interface objects.
The methods in the interface are implemented by the class.
The access modifier is always public.
The variables defined in the interface are always constants.
The constant in the interface is modified by public static final.
Interface can implement multi-inheritance.
Interface implementation multi-Inheritance
Syntax:
[Access modifier] interface Name extends interface 1, interface 2... {}
The implemented interface is implemented using the implements keyword.
Syntax:
[Access modifier] class name implements interface name {}
To implement an interface, you must implement all methods in the interface. Otherwise, the current class must be defined as an abstract class.
Static keywords
Prerequisites:
Instance variables and class variables
Instance variables are called through class objects.
A class variable is called directly by class name without calling through class objects.
Purpose:
Static can modify variables to be static variables or class variables.
Static modified variables belong to a class and do not belong to an object. No matter how many objects are created, static variables have only one copy. That is to say, all objects use the same class variable.
Static variable can be called directly by class name.
The static modification method becomes a class method, and the static method can be called directly by class name.
Static can modify the code block, which is used to initialize static variables.
Syntax:
static{}
Features:
Only static methods or variables can be called in static methods.
You can call static methods or static variables in non-static methods.
This and super keywords cannot be used in static methods.
Local variables cannot be modified statically.