Java introductory notes

Posted by Daveg on Thu, 04 Nov 2021 04:39:17 +0100

Introduction notes to Java (II)

1. Polymorphic mechanism

What is polymorphism is nothing more than multiple forms of a thing. In fact, it is easy to understand. Give the following examples:

//Parent class
public class Animal {
    public void show() {
        System.out.println("I'm an animal.");
    }
}
//Cat subclass
public class Cat extends Animal {
    public void show() {
        System.out.println("I'm a cat.");
    }
}
//Dog subclass
public class Dog extends Animal {
    public void show() {
        System.out.println("I'm a dog.");
    }
}
//demo method call class
 public static void show(Animal animal) {
       animal.show();
 }
 public static void main() {
     Dog d = new Dog();
     show(d);
 }
//The results are as follows:
//For the show method of demo class, if the Animal parameter is Dog, the method in Dog will be called. If it is Cat, the method in Cat will be called.

Benefits of polymorphism: it is convenient for the project to extend subclasses and reuse public functions in parent classes. Disadvantages: you cannot access subclass independent properties or methods.

In polymorphism, there are subclass like parent transformation and parent like subclass transformation.

//The subclass is like the abstraction of the parent class
Animal a=new Dog();//The effect is that the property access in the obtained Animal class a still comes from the parent class, but the called method is overridden by the child class.
//The parent class is like the materialization of a child class (it can also become a return child class object)
Dog b=(Dog)a;//The obtained b will return all the features of the a Dog class and will not open new memory

2. Usage characteristics of abstract classes

Polymorphism extends an abstract class, mainly in practical logic. For an abstract parent class of Animal, its methods have no practical significance, such as "animals eat food". Because there is no specific subclass, it is not necessary to write specific methods. However, in order to ensure that the method subclasses of the parent class have, abstract classes appear. Their basic characteristics are: they also have construction methods. As long as the subclass contains abstract methods, the parent class must be defined as an abstract class, and the abstract methods must be overridden by the subclass. Use the following:

//Parent class
public abstract class Animal {
    String name="animal";
    Animal(){
        System.out.println("Constructor of parent class");
    }
    Animal(String name){
       this.name=name;
    }
    public abstract void show();
}
//Subclass
public class Cat extends Animal {
    String name = "cat";
    public void show() {//The show method must be added
        System.out.println("I'm a cat.");
    }
}
//plus: subclasses of abstract classes can also be abstract classes

3. Basic usage of interface

Interfaces are more like abstract classes. It can be said that abstract classes implement the abstraction of a class (including class properties and behavior), while interfaces are mainly the abstraction of class behavior. It should be noted that the attribute constant type in the interface is an abstract method. Examples are as follows:

//Define an interface interface1
public interface interface1 {
    public final int test=1;//Equivalent to int test=1;
    public abstract void inter();//Equivalent to void inter();
    //In idea, it will be reported that public final and public abstract are redundant modifications
}
//Abstract interfaces cannot be used directly. You need to implement the corresponding interface in the class and then call it by instantiating the class
//The corresponding interface is implemented in the dog class
public class Dog extends Animal implements interface1{
    @Override//Note: the corresponding abstract function must be implemented, unless the Dog class is also set as an abstract class
    public void inter() {
        System.out.println("Dog Class interface1 Interface");
    }
}
//Interface used in demo class
interface1 test=new Dog();
test.inter();

It should be noted that in terms of inheritance, compared with abstract classes, interfaces can realize multi-level inheritance and direct multi inheritance.

//Multiple implementation of class interface
public class Dog extends Animal implements interface1,interface2,interface3
//Direct multiple inheritance of interfaces (the above code is equivalent to)
public interface interface3 extends interface1,interface2//Interface multi inheritance
public class Dog extends Animal implements interface3//Class implementation of interface

Finally, like classes, interfaces can also be passed as parameters in functions

//Function writing method of interface as parameter: < interface name: formal parameter >
public static void interfaceFun(interface2 test ){
        test.show2();
}
//Call mode
public static void main(String [] args) {
    interface2 test=new Dog();//The interface must be implemented in the Dog class to be called normally
    interfaceFun(test);
}

4. Summary of this

This in Java is not as flexible as this in JavaScript and can only be used in constructors. In addition, it should be noted that the following description of this in Java is wrong:

In fact, the reason is also well understood. If the constructor called is not the first one, the property modification on the constructor is invalid.

Inside. In addition, it should be noted that the following description of this in Java is wrong:

[external chain picture transferring... (img-DfI9LYis-1635998807418)]

In fact, the reason is also well understood. If the constructor called is not the first one, the property modification on the constructor is invalid.

Topics: Java Back-end