2022Java learning notes 36 (object-oriented enhancement: abstract classes, abstract class cases)

Posted by pragma on Sun, 20 Feb 2022 13:26:02 +0100

2022Java learning notes 36 (object-oriented enhancement: abstract classes, abstract class cases)

1, Abstract class

A parent class knows that all its subclasses need to complete a function, but the completion of each subclass is different. The parent class can only define the basic requirements of the function for a long time, and the specific implementation is completed by the subclass. This class can be an abstract class, which is actually an incomplete design drawing

Abstract class definition format:

Modifier  abstract class Class name{}

Abstract method:

It is the basic requirement of the functions that must be completed by the subclasses defined in the abstract class
There is no method body, only method signature, which must be modified by abstract

Modifier  abstract Return value type method name(parameter list );

Example code
Define parent class
If the method is modified with abstract, the class should also be modified with abstract, otherwise an error will be reported

package com.zcl.d6_abstract_class;

public abstract class Animal {
    // Parent class
    public String naem;

    // Abstract method signature
    public abstract void run();

    public String getNaem() {
        return naem;
    }

    public void setNaem(String naem) {
        this.naem = naem;
    }
}

Defines a subclass that inherits the Animal parent class
You need to inherit the Animal parent class and override the abstract method run()
If you do not override the method, you need to modify the subclass with abstract

package com.zcl.d6_abstract_class;

public class Tiger extends Animal{
    // If the method is not rewritten, an error will be reported
    @Override
    public void run() {
        System.out.println("The tiger runs to the thief. Come on");
    }
}

Test class

package com.zcl.d6_abstract_class;

public class Test {
    public static void main(String[] args) {
        Tiger t = new Tiger();
        t.run(); // The tiger runs to the thief. Come on
    }
}

Summary and precautions of abstract use
1. Abstract classes are used to be inherited, and abstract methods are implemented by subclasses
2. If a class inherits an abstract class, the class must override all abstract methods of the abstract class, otherwise the class must also be defined as an abstract class

2, Abstract class understanding case

code implementation
Define abstract parent class

package com.zcl.d7_abstract_test;

/*
    Abstract parent class
 */
public abstract class Card {
    private String name; // Card owner
    private double money; // balance

    /*
        Subclasses must have payment function. The payment situation of each subclass is different, so the parent class defines the payment parent class as an abstract method and gives it to the specific subclass for implementation
     */
    public abstract void pay(double money);

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

Define gold card consumption

package com.zcl.d7_abstract_test;

/**
 *  golden card
 */
public class GoLDgARD extends Card {
    // Override payment parent class abstract method
    @Override
    public void pay(double money) {
        // Calculate the amount after discount
        double rs = money * 0.8;
        // Balance after consumption
        double lastMoney = getMoney() - rs;
        System.out.println(getName()+"Total amount of current account:"+getMoney()+",The current consumption amount is:"+rs+",Balance remaining after consumption:"+lastMoney);
        // Modify the balance in the account after outputting the data
        setMoney(lastMoney);
    }
}

Define silver card consumption

package com.zcl.d7_abstract_test;
/*
    Define silver card consumption method
 */
public class SilverCard extends Card{
    @Override
    public void pay(double money) {
        // Calculate the amount after discount
        double rs = money * 0.85;
        // Balance after consumption
        double lastMoney = getMoney() - rs;
        System.out.println(getName()+"Total amount of current account:"+getMoney()+",The current consumption amount is:"+rs+",Balance remaining after consumption:"+lastMoney);
        // Modify the balance in the account after outputting the data
        setMoney(lastMoney);
    }
}

Define test consumer classes

package com.zcl.d7_abstract_test;
/*
    Test class
 */
public class Test {
    public static void main(String[] args) {
        // Define the object of the gold card
        GoLDgARD c = new GoLDgARD();
        c.setMoney(10000); // Define the amount of the amount
        c.setName("Zhang San"); // Card owner
        c.pay(300); // Consumption amount
        // The total amount of Zhang San's current account is 10000.0, the current consumption amount is 240.0, and the remaining balance after consumption is 9760.0

        // Define silver card calculation object
        SilverCard s = new SilverCard();
        s.setMoney(10000);
        s.setName("Li Si");
        s.pay(500);
        // The total amount of Li Si's current account is 10000.0, the current consumption amount is 425.0, and the remaining balance after consumption is 9575.0
    }
}

3, Characteristics and precautions of abstract classes

features:
1. When you create an abstract method, you lose the ability to create objects
2. Classes include member methods, variables, constructors, and abstract classes. If you create an object, you can abstract a method through the object method, and the abstract method is only a method signature without a specific execution code block
3. Abstract classes do not necessarily have abstract methods. If there are abstract methods, they must be abstract classes
4. A class inherits an abstract class. You must rewrite all the abstract methods in the abstract class, otherwise the class must also be an abstract class
5. abstract cannot be used to modify variables, code blocks and constructors

4, What is the relationship between final and abstract

1. Mutually exclusive relationship
2. The abstract method defined by abstract is used as a template for subclasses to inherit. The class defined by final cannot i be inherited
3. The general function of abstract method definition allows subclasses to be rewritten. The subclass of method defined by final cannot be rewritten

Topics: Java JavaEE