"Java Programming Thought" Notes Chapter 9 Interface

1. Abstract classes and abstract methods

1.1 Abstract method, only method declaration without method body

abstract class AbstractClass{
	abstract void f(); //no {}
	
}

1.2 As long as there is one or more abstract methods, it must be an abstract class, and an abstract class can have no abstract methods.

1.3 There can be no abstract methods in an abstract class, or only implemented methods.

abstract class AbstractClass{
	
	void g(){
		System.out.println("hello");
	}
}

1.4 Inheriting an abstract class, it is necessary to override all abstract methods in the abstract class and provide the method body. If the method body is not provided, the subclass is also an abstract class, and a keyword should be added in front of it.

2. Interface

2.1 There is no concrete implementation in the interface, and there is no method body.

2.2 Default public permissions for methods in interfaces

2.3 Interfaces can have fields, but are implicitly set to static and final

2.4 Interface permissions are also public like classes, but they must be in a file with the same name

3. Covariant type:

  • The return type of the overridden method in the  subclass can be a subtype of the return value of the superclass method
class A {

}

class B extends A {

}

abstract class AbstractClass {

	abstract A g();

}

class C extends AbstractClass {
	//The return value type is type A
	// A g(){   
	// return null;
	// }
	//g() The return value type is a subtype B of type A can also be overridden
	B g() {
		
		return null;

	}
}

4. Multiple inheritance

4.1 Interface extends interface, interface, interface, . . .

4.2 class extends interface, interface, interface, . . .

4.3 Class extends Class implements interface, interface, interface, . . .

4.4 Class implements interface, interface, interface, . . .

  • Implement multiple interfaces, which should avoid having the same method name

5. Nested Interfaces

  •  Interfaces can be nested within classes or within other interfaces

5.1 Treat nested interfaces/classes as data members of class A outside of class A, and as normal interfaces/classes inside of class A

5.2 The class or interface permission nested in the class can be private

5.3 Since the methods in the interface are public by default, the interface in the interface cannot be declared private

5.4 The function of the private interface in nesting is to make the method in this private interface have no return type and cannot be upcast

class A {
	public class Ain{
		
		 
		}
	//Internal class permissions can be set to private
	private class Ain2{  
		
		}
	public interface Aface {
		
	}
	private interface Aface2{
		void f(); //must return null
	}
	
}

6. Interface and Factory

  •     To be added

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392081&siteId=291194637