Basic Java learning 11

Keywords: Java


Interface is a reference type in the Java language and a collection of methods. If a class encapsulates member variables, construction methods and member methods, the interior of the interface mainly encapsulates methods, including abstract methods (JDK 7 and before), default methods and static methods (JDK 8) and private methods (JDK 9). Interface, which is similar to defining classes, but uses the interface keyword. It will also be compiled into a. Class file, but it must be clear that it is not a class, but another reference data type.

  • Reference data types: array, class, interface.

Interface, which cannot create objects, but can be implemented (similar to being inherited). 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.

  • Contains abstract methods
    Abstract method: use the abstract keyword to modify. It can be omitted. There is no method body. This method is used by subclass implementations.
public interface InterFaceName { 
	public abstract void method(); 
  • Contains default and static methods
    Default method: use the default modifier and cannot be omitted. It can be called or overridden by subclasses.
    Static method: static modification is used for the interface to call directly.
public interface InterFaceName { 
	public default void method() { 
			// Execute statement 
	public static void method2() { 
			// Execute statement 
  • Contains private methods and private static methods
    Private method: private modification is used for calling the default method or static method in the interface.
public interface InterFaceName { 
    private void method() {
   	 // Execute statement 

Use of abstract methods: all must be implemented
Use of default methods: you can inherit, override, or choose one, but you can only call it through the object implementing the class.
Use of static methods: static methods are related to. Class files. They can only be called using the interface name, not through the class name of the implementation class or the object of the implementation class
Use of private methods:

  • Private method: only the default method can be called.

  • Private static methods: default methods and static methods can be called.

    If there are multiple default methods in an interface and there are duplicate contents in the methods, they can be extracted and encapsulated into private methods for the default methods to call. From a design perspective, private methods are an aid to default and static methods.


Polymorphism is the third feature of object-oriented after encapsulation and inheritance. In life, such as running, kittens, dogs and elephants run differently. Another example is the action of flying. Insects, birds and planes fly differently. It can be seen that the same behavior can reflect different forms through different things. Polymorphism describes such a state.

  • Polymorphism: refers to the same behavior with multiple different manifestations.

Premise [key]

  1. Inherit or implement [one of two]
  2. Rewriting of methods [meaning embodiment: no rewriting, meaningless]
  3. The parent class reference points to the child class object [format representation]

Benefits of polymorphism

In the actual development process, the parent class type is used as the formal parameter of the method, passing the subclass object to the method and calling the method, which can better reflect the expansibility and convenience of polymorphism. Therefore, the benefits of polymorphism are that it can make the programming simpler and have good expansion.

Reference type conversion

Polymorphic transformation can be divided into upward transformation and downward Transformation:

  • Upward Transformation: polymorphism itself is the process of upward transformation from subclass type to parent type, which is the default. When the parent class reference points to a subclass object, it is an upward transformation.
//Test10  Test11 Test12
//Polymorphism of methods
//  |~Method overloading: the same method name can call different method bodies according to the type and number of parameters
//  |~Method override: methods of the same parent class may have different implementations according to different instantiated subclasses
//Polymorphism of object (premise: Method override):
//  Automatic | ~ object upward Transformation: parent object = child instance;
//  [force] | ~ object downward Transformation: subclass subclass object = (subclass) parent class instance;
class A {
    public void print(){
        System.out.println("[A]public void print(){}");
class B extends A {
    public void print() {
        System.out.println("[B]public void print(){}");
    public void funB(){//This method has only subclasses
        System.out.println("[B]public void funB(){}");
public class Day10 {
    public static void main(String[] args) {
        A a = new B();//Instantiate subclass objects
        System.out.println(a instanceof A);//instanceof judgment
        System.out.println(a instanceof B);//The ClassCastException problem is avoided
        a.print();//Call overridden method
        if (a instanceof B){
            B b = (B)a;//Downward transformation
            b.funB();//Not all parent classes can be transformed downward,

// Only after upward transformation can we transform downward, for example:
//        A a = new A();
//        B b = (B)a;
//        Error: ClassCastException

Posted by sethadam1 on Sun, 03 Oct 2021 16:32:30 -0700