Basic knowledge of Java --- explanation of abstract classes!!!

Posted by marsupillami on Wed, 29 Dec 2021 22:31:21 +0100

abstract class

Overview of abstract classes

The class used to describe abstract concepts is called abstract class. The methods in abstract class do not have to realize specific functions.

characteristic

1. Modified by abstract keyword
2. Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be defined as abstract classes.
3. Abstract classes have construction methods, but they cannot be created objects. Construction methods are called by subclasses.
4. Abstract classes can be inherited by standard classes or abstract classes. However, the standard class must override the abstract method in the abstract class, but the abstract class does not need it.

package org.wdzl.unit04;

/**
 * Abstract class:
 *   Overview: classes used to describe abstract concepts are called abstract classes. Methods in abstract classes do not have to implement specific functions.
 *   Features: 1. Modified by abstract
 *        2,Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be defined as abstract classes.
 *        3,Abstract classes cannot be instantiated
 *           But abstract classes have construction methods, which are called by subclasses.
 *        4,Abstract classes can be inherited.
 *           a,Subclasses must implement abstract methods in abstract classes.
 *           b,Abstract classes can inherit abstract classes and do not need to implement abstract methods in the parent class.
 *        Polymorphism: different states of the same object at different times.
 *        Application of polymorphism: it is mainly applied to the inheritance relationship between abstract classes and standard classes.
 *
 */
public class AbstractDemo {
    public static void main(String[] args) {
        //Pests pests = new Pests(); // Compile time exception
        Pests pests = new Cat();
        pests.eat();
        pests.voice();
    }
}
abstract class Pests{
    //Abstract method
    public abstract void eat();
    //Abstract method
    public abstract void voice();
}
class Dog extends Pests{

    @Override
    public void eat() {
        System.out.println("Eat shit");
    }

    @Override
    public void voice() {
        System.out.println("Woof, woof");
    }
}
class Cat extends Pests{

    @Override
    public void eat() {
        System.out.println("Eat fish");
    }

    @Override
    public void voice() {
        System.out.println("cat ");
    }
}

Member characteristics

1. Member variable
It can be a variable or a variable (constant) modified by final
2. Construction method
There are construction methods for subclass calls to initialize parent class data.
3. Member method
It can be an abstract method or a non abstract method

package org.wdzl.unit04;

/**
 * Composition of abstract classes:
 *       1,Member variable
 *           It can be a variable or a variable (constant) modified by final.
 *       2,Member method
 *           It can be abstract and non abstract methods.
 *           Abstract method: force subclasses to implement this method.
 *           Non abstract method: the method is inherited by subclasses to improve the reusability of code.
 *       3,Construction method
 *          It can be constructed with or without parameters, which is used to initialize the subclass to access the parent class data.
 */
public class AbstractDemo2 {
    public static void main(String[] args) {
        Animal animal = new Pig();
        System.out.println(animal.num);
        System.out.println(animal.num2);
        animal.run();
    }
}
abstract class Animal{
    public int num = 10;
    public final int num2 = 20;
    public Animal(){
        System.out.println("Animal Nonparametric structure");
    }
    public Animal(int num){
        System.out.println("Animal Parametric structure");
    }
    public abstract void eat();
    public void run(){
        System.out.println("Run, run");
    }
}
class Pig extends Animal{
    public Pig(){
        super(3);
    }
    @Override
    public void eat() {
        System.out.println("be usurious");
    }
}

matters needing attention:

1. Abstract classes have construction methods, but they cannot be instantiated. Their function is to initialize parent class data for subclass calls.
2. If a class has no abstract method, it can also be defined as an abstract class. Its purpose is to prevent the class from creating objects.
3. Relationship between abstract keyword and other keywords
a. Private: conflict, because the member method modified by private cannot be inherited.
b. Final: conflict, because the member method modified by final cannot be overridden.
c. Static: conflict, because the member methods modified by static are loaded with the loading of the class, but the member methods modified by abstract are not implemented, so such a combination is meaningless.

package org.wdzl.unit04;

/**
 * Precautions for abstract classes;
 *     1,A class without an abstract method can also be defined as an abstract class. The purpose of this is to prevent the class from creating objects.
 *     2,abstract Relationship with other keywords
 *          a,private: Conflict relationship, because abstract methods must be implemented by subclasses, and private modified methods cannot be inherited, resulting in
 *                     This method cannot be overridden by subclasses.
 *          b,final: Conflict relationship, because when final modifies a method, the method cannot be overridden,
 *          c,static: Conflict relationship, because the static modifier is loaded with the loading of the class. We can load such modifiers through the class name
 *                    Combination is meaningless.
 *
 */
public class AbstractDemo4 {
    public static void main(String[] args) {

    }
}
abstract class Demo{
//    private abstract void showInfo();// Compile time exception
//    final abstract void showInfo();// Compile time exception
//      static abstract void showInfo();// Compile time exception
}

Topics: Java abstract class