catalogue
Chapter 1 abstract classes and interfaces
1.2 use of abstract classes and methods
2.2 definition format of interface
2.3 definition and use of interfaces containing abstract methods
2.4 definition and use of default method in interface
2.5 definition and use of static methods in interfaces
2.6 definition of private method in interface
2.7 definition and use of constants in interfaces
Chapter 2 final, permission, internal class and reference type
1.2 definition of four modification types of final keyword
1.2 how to use the four modification types of final keyword
2.2 access capabilities with different permissions
Statement: This article is compiled in the Java teaching video of dark horse programmer, which can only be used as a reference for learning. Please contact us to delete the infringement.
Chapter 1 abstract classes and interfaces
1, Abstract class
1.1 general
Methods without method bodies are called abstract methods, and classes containing abstract methods are abstract classes.
Abstract method: a method without a method body. [that is, with the abstract keyword, remove the braces and end directly with a semicolon]
Format:
Modifier abstract Return value type method name (parameter list);
Abstract class: a class that contains abstract methods. [the class of the abstract method must be an abstract class.]
Format:
abstract class Class name { }
1.2 use of abstract classes and methods
1) new abstract class objects cannot be created directly.
Student student = new Student();//Wrong writing, cannot create abstract class object directly
2) You must use a subclass to inherit the abstract parent class.
3) Subclasses must override all abstract methods in the abstract parent class.
4) Create subclass objects for use.
2, Interface
2.1 general
1) Interface is a reference type in Java language and a collection of methods. The interior of the interface mainly encapsulates methods, including abstract methods (JDK 7 and before), default methods, static methods (JDK 8) and private methods (JDK 9).
2) The definition of the interface is similar to that of the class, but the interface keyword should be used. It will also be compiled into Class file, but it is not a class, but another reference data type.
Reference data types: array, class, interface.
3) An interface cannot create an object, but it can be implemented (similar to being inherited).
4) A class that implements an interface (which can be regarded as a subclass of the interface) needs to implement all abstract methods in the interface. After creating this class object, you can call the method, otherwise it must be an abstract class.
2.2 definition format of interface
public interface Interface name { // Content of interface }
2.3 definition and use of interfaces containing abstract methods
2.3. 1 interface definition with abstract methods
1) Format
public abstract Return value type method name(parameter list);
2) Precautions
① The modifier of the abstract method in the interface must be the fixed keyword public abstract.
② The public abstract keyword can be omitted.
③ The three elements of the method can be defined at will.
3) Example
public interface MyInterface_abstract { //Four types of abstract methods public abstract void method_1(); abstract void method_2(); public void method_3(); void method_4(); }
2.3. 1 use of abstract class interface
1) The interface cannot be used directly. There must be an implementation class to implement the interface.
public class Implementation class name implements Interface name { //... }
2) The implementation class of the interface must override all abstract methods in the rewriting interface.
Implementation: remove the abstract keyword and add method body braces.
3) Create objects that implement classes for use.
① Writing implementation classes
public class MyInterface_abstractImpl implements MyInterface_abstract { @Override public void method_1() { System.out.println("Method 1"); } @Override public void method_2() { System.out.println("Method 2"); } @Override public void method_3() { System.out.println("Method 3"); } @Override public void method_4() { System.out.println("Method 4"); } }
② Create a test class to use the abstract class.
public class Interface_abstract { public static void main(String[] args) { //Create an implementation class object for use MyInterface_abstractImpl impl = new MyInterface_abstractImpl(); impl.method_1();//Method 1 impl.method_2();//Method 2 impl.method_3();//Method 3 impl.method_4();//Method 4 } }
Note: if the implementation class does not cover all abstract methods in the rewriting interface, the implementation class itself must be an abstract class.
2.4 definition and use of default method in interface
2.4. 1. Definition of default method in interface
1) Format
public default Return value type method name (parameter list) { Method body }
2) Example
public interface MyInterface_default { //Default method public default void method_default() { System.out.println("Default method"); } }
2.4. 2. Use of default methods in the interface
1) Create implementation class
public class MyInterface_defaultImpl_A implements MyInterface_default{ @Override public void method_abstract() { System.out.println("Implementation class A"); } } public class MyInterface_defaultImpl_B implements MyInterface_default{ @Override public void method_abstract() { System.out.println("Implementation class"); } @Override public void method_default() { System.out.println("Implementation class B Override overrides the default method"); } }
2) Create test class test default method
public class Interface_abstract { public static void main(String[] args) { //Create an implementation class object for use MyInterface_defaultImpl_A a = new MyInterface_defaultImpl_A(); a.method_abstract(); a.method_default(); System.out.println("-------------------------"); MyInterface_defaultImpl_B b = new MyInterface_defaultImpl_B(); b.method_abstract(); b.method_default(); } }
2.5 definition and use of static methods in interfaces
2.5. 1 definition of static method in interface
1) Format
public static Return value type method name (parameter list) { Method body }
That is, replace abstract or default with static, with the upper body.
2) Example
public interface MyInterface_static { public static void method_static(){ System.out.println("Static method"); } }
2.5. 2 use of static methods in the interface
Note: static methods in the interface cannot be called through the object implementing the class, but directly through the interface name.
Create test classes using static methods
public class Interface_static { public static void main(String[] args) { MyInterface_static.method_static(); } }
2.6 definition of private method in interface
2.6. 1 definition of private method in interface
1) Format
//Common private method private Return value type method name (parameter list) { Method body } //Static private method private static Return value type method name (parameter list) { Method body }
2) Example
public interface MyInterface_private { private void methodStatic() { System.out.println("Private method"); } }
2.7 definition and use of constants in interfaces
2.7. 1 definition of constants in the interface
1) Format
public static final Data type variable name = Data value;
Once the final keyword is used, the description cannot be changed.
2) Precautions
① public static final can be omitted for constants in the interface.
② Constants in the interface must be assigned values.
③ The names of constants in the interface are fully capitalized and separated by underscores.
3) Example
public static final int i = 12;
2.7. 2 use of constants in the interface
1) Create interface
public interface MyInterface_const { public static final INT i = 12; }
2) Create implementation class access constants
public class Interface_const { public static void main(String[] args) { //Constants in the provider System.out.println(MyInterface_const.INT); } }
Chapter 2 final, permission, internal class and reference type
1, final keyword
1.1 general
A subclass can overwrite the contents of the parent class on the basis of the parent class. In order to avoid this random inheritance of the classes provided in the API and rewrite their contents, Java provides the final keyword to modify the unchangeable contents.
final: cannot be changed. Can be used to decorate classes, methods, and variables.
Class: a modified class that cannot be inherited.
Method: the modified method cannot be overridden.
Variable: a modified variable that cannot be re assigned.
1.2 definition of four modification types of final keyword
1) Decorate a class
final class Class name {}
① A final decorated class cannot have subclasses.
② All member methods of the final modified class cannot be overridden.
③ The final modified class can override the member methods of the parent class.
2) Modify a method
Modifier final Return value type method name(parameter list){ //Method body }
Note: for classes and methods, the abstract keyword and the final keyword cannot be used at the same time.
3) Modify a local variable
final Data type variable name = Data value;
4) Modify a member variable
final Data type variable name (all English uppercase) = Data value;
① Member variables have default values, so they must be assigned manually after final modification.
② For the member variable modified by final, it is either assigned directly or through the construction method.
③ It must be ensured that all overloaded construction methods in the class will eventually assign values to the member variables of final.
1.2 how to use the four modification types of final keyword
1) Decorate a class
//final modifier class public final class MyFinalClass { public void method () { System.out.println("final Modifier class"); } }
2) Modify a method
//Parent class public class Fu { public final void method() { System.out.println("Parent class method execution!"); } } //Subclass public class Zi extends Fu{ @Override public void method() {//The program reports an error because the method method is modified by final and cannot be overwritten super.method(); } }
3) Modify a local variable
//Create Student class public class Student { private String name; public Student(String name) { this.name = name; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } } //Create Final class public class Final { public static void main(String[] args) { final int num = 10; System.out.println(num); final Student student1 = new Student("Zhang San"); System.out.println(student1.getName()); //student1 = "Wang Wu"// The program reports an error, and the address value of the local variable modified by final cannot be changed. student1.setName("Wang Wu"); System.out.println(student1.getName()); } }
4) Modify a member variable
//Direct assignment public class Person { private final String name = "Li Si"; public String getName() { return name; } public Person() { } } //Assignment by construction method public class Person { private final String name; public String getName() { return name; } /* public void setName(String name) { this.name = name; }*/ public Person(String name) { this.name = name; } public Person() { name = "Li Si"; } }
2, Authority
2.1 general
Four kinds of access rights are provided in Java. When modified with different access rights modifiers, the modified content will have different access rights.
Public: public
Protected: protected
Default: default
Private: private
2.2 access capabilities with different permissions
public | protected | default | private | |
---|---|---|---|---|
Same class | √ | √ | √ | √ |
Same package | √ | √ | √ | |
Subclasses of different packages | √ | √ | ||
Non subclasses of different packages | √ |
Suggestion: use private for member variables to hide details; The construction method uses public to facilitate the creation of objects; Member methods use public, which is convenient for calling methods. Without permission modifier, its access ability is the same as that of default modifier
3, Inner class
3.1 general
Define a Class A in another class B, which class A is called the inner class and B is called the outer class.
3.2 member internal class
1) Overview
Defines a class outside a method in a class.
2) Define format
class External class { class Inner class{ } }
3) Characteristics
① Internal classes can directly access members of external classes, including private members.
② If an external class wants to access the members of an internal class, it must create an object of the internal class.
Create internal class object format:
External class name.Internal class name object name = new External type().new Internal type();
4) Examples
① Create Person class
public class Person { private boolean live = true; class Heart { public void jump() { // Direct access to external class members if (live) { System.out.println("The heart is beating"); } else { System.out.println("The heart stopped beating"); } } } public boolean isLive() { return live; } public void setLive(boolean live) { this.live = live; } }
② Create test class
public class PersonImpl { public static void main(String[] args) { // Create an external class object Person p = new Person(); // Create an internal class object Person.Heart heart = p.new Heart(); // Calling internal class methods heart.jump(); // Call external class methods p.setLive(false); // Calling internal class methods heart.jump(); } }
3.3 anonymous inner class
1) Overview
Anonymous inner classes are simplified versions of inner classes. Its essence is an anonymous subclass object with a concrete implementation parent class or parent interface.
Premise: an anonymous inner class must inherit a parent class or implement a parent interface.
2) Define format
new Parent class name or interface name(){ // Method rewrite @Override public void method() { // Execute statement } };
① new represents the action of creating an object
② The interface name is the interface that the anonymous inner class needs to implement
③ Inside the braces are the contents of the anonymous inner class
3) Precautions
① Anonymous inner classes can only be used once when creating objects. If you want to create an object multiple times and the contents of the class are the same, you must use a separately defined implementation class.
② When an anonymous object calls a method, it can only be called once. If you want the same object to call methods multiple times, you must give the object a name.
③ Anonymous inner classes omit implementation class / subclass names, but anonymous objects omit object names.
Emphasize: anonymous inner classes and anonymous objects are not the same thing!