Abstract classes of JavaSe

Keywords: Java

Characteristics of abstract classes:
1. Abstract classes and methods must be decorated with the abstract keyword
Format:
abstract class class name {}
abstract void eat();
2. Abstract classes do not necessarily have abstract methods. Classes with abstract methods must be abstract classes
Abstract classes can have concrete implementation methods
3. Abstract classes cannot be instantiated
So, how to instantiate abstract classes?
In a polymorphic way, there must first be an inheritance relationship, which is instantiated by a specific subclass.
The formal instantiation of polymorphism is called abstract class polymorphism
4. Subclasses of abstract classes
1. If it is a concrete subclass, you must override all abstract methods
2. If it is an abstract subclass, you can override the abstract method of the parent abstract class or selectively

//Because the class has abstract methods, the class must be defined as an abstract class
//class Demo{
//    public abstract void show();
//}

abstract class Demo{
    public abstract void show();

    public void show2(){
        System.out.println("This is a concrete method in an abstract class");
    }
}


//Define an abstract class
abstract class Animal{
    //Define an abstract method called eat. There is no method body
    public abstract void eat();
    //The following definition of an abstract method is wrong, {} represents an empty method body and will report an error. An abstract method cannot have a method body
//    public abstract void eat(){};

    //Define an abstract method called sleep. There is no method body
    public abstract void sleep();
}

//abstract class Demo2 extends Animal{
//    @Override
//    public void eat() {
//        System.out.println("rewrite method");
//    }
//}

//Shortcut alt + enter
class Cat extends Animal{

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

    @Override
    public void sleep() {
        System.out.println("Sleep on your stomach");
    }
}



public class AbstractDemo1 {
    public static void main(String[] args) {
        //The class Animal is an abstract class, which cannot be instantiated
//        Animal animal = new Animal();

//        Cat cat = new Cat();
//        cat.eat();
//        cat.sleep();

        //Instantiate in polymorphic form
        Animal a1 = new Cat();
        a1.eat();
        a1.sleep();
    }
}

Characteristics of members in abstract classes:
Member variable: it can be a variable or a constant
Construction method: there are construction methods
But abstract classes cannot be instantiated. What's the use of having a constructor?
Used for subclass access to parent class for initialization
Member method:
It can be a concrete implementation method or an abstract method

        characteristic:
            1,If it is a concrete subclass, you must override all abstract methods
            2,If it is an abstract subclass, you can override the abstract method of the parent abstract class or selectively
abstract class Animal2{
    int num  = 20;
    final int num2 = 200;

    Animal2(){}

    abstract public void sleep();

}

class Dog extends Animal2{

    @Override
    public void sleep() {
        System.out.println("Sleep sideways");
    }
}


public class AbstractDemo2 {
    public static void main(String[] args) {
        Animal2 a = new Dog();
        a.sleep();
    }
}

If a class has no abstract methods, can it be defined as an abstract class?
sure
If so, what's the point?
If there are a lot of things, they all have common concepts. This common concept itself has no specific existence in reality. Put these concepts together
They are grouped into one class, which is an abstract class.
As for whether this abstract class has methods, it depends on whether a lot of things have common behavior, if they just have the same essence.
However, it is also reasonable that there is no common behavior.

    Summary:
        1,Abstract classes can have no abstract methods
        2,Abstract classes cannot be instantiated

    abstract Which keywords cannot coexist with
        private  conflict
        final    conflict
        static   conflict
abstract class Demo2{
    public abstract void show();

    //Illegal modifier combination: abstract and private
//    private abstract void show2();

    //Illegal modifier combination: abstract and final
//    final abstract void show3();

    //Illegal modifier combination: abstract and static
//    static abstract void show4();

}

public class AbstractDemo3 {
    public static void main(String[] args) {

    }
}

Abstract class exercise 1:
Cat and dog case
Specific things: cat, dog
Commonness: name, age, eating

    Analysis: the process from concrete to abstract
        Cat:
            Member variables: name, age
            Construction method: nonparametric construction and parametric construction
            Member method: eat (cat eats fish)

        dog:
            Member variables: name, age
            Construction method: nonparametric construction and parametric construction
            Member method: eat (dogs eat meat)

   Through analysis, it is found that cats and dogs have some common contents, so we extract a parent class: animal class according to these common contents: Animal
   However, due to the different ways of eating, we define the method of eating as an abstract method.
   Because the method of eating is an abstract method, this class must be defined as an abstract class

   Abstract animal classes: Animal
        Member variables: name, age
        Construction method: nonparametric construction and parametric construction
        Member method: eating is an abstract method eat();

   Implementation: from abstract to concrete
        Abstract animal classes: Animal
           Member variables: name,age;
           Construction method: nonparametric construction and parametric construction
           Member method: eating is an abstract method public abstract void eat();
        Dogs:
            Automatic inheritance class
            Parametric structure
            Override abstract methods
        Cats:
            Automatic inheritance class
            Parametric structure
            Override abstract methods
abstract class Animal3{
    private String name;
    private int age;

    public Animal3() {
    }

    public Animal3(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //Is an abstract method public abstract void eat();
    public abstract void eat();
}


class Dog3 extends Animal3{

    public Dog3() {
    }

    public Dog3(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("Dogs eat meat");
    }
}

class Cat3 extends Animal3{

    public Cat3() {
    }

    public Cat3(String name, int age) {
        super(name, age);
    }

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


public class AbstractTest {
    public static void main(String[] args) {
        Animal3 a = new Cat3("floret",3);
        System.out.println(a.getName()+"---"+a.getAge());
        a.eat();

        Animal3 a2 = new Dog3("chinese rhubarb",4);
        System.out.println(a2.getName()+"---"+a2.getAge());
        a2.eat();

    }
}

Teacher case
Specific things: Thirteen teachers, twelve teachers
Commonalities: name, age, lecture.

    analysis:
        Thirteen teachers:
            Name, age
            Lecture (Lecture) JavaSE)

        Twelve teachers:
            Name, age
            Lecture (Lecture) Hive)

    realization:
        Abstract teacher class:
            Name, age
            Lectures are defined as abstract methods
        Thirteen teachers:
            Inherited from teacher class
            Name, age
            Lecture (Lecture) JavaSE)
        Twelve teachers:
            Inherited from teacher class
            Name, age
            Lecture (Lecture) Hive)
abstract class Teacher{
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public abstract void teach();
}

class ThirteenTeacher extends Teacher{

    public ThirteenTeacher() {
    }

    public ThirteenTeacher(String name, int age) {
        super(name, age);
    }

    @Override
    public void teach() {
        System.out.println("teach JavaSE");
    }
}

class TwelveTeacher extends Teacher{

    public TwelveTeacher() {
    }

    public TwelveTeacher(String name, int age) {
        super(name, age);
    }

    @Override
    public void teach() {
        System.out.println("teach Hive");
    }
}

public class AbstractTest2 {
    public static void main(String[] args) {
        Teacher t1 = new ThirteenTeacher("Little tiger",24);
        System.out.println(t1.getName()+"---"+t1.getAge());
        t1.teach();

        Teacher t2 = new TwelveTeacher("Fire",24);
        System.out.println(t2.getName()+"---"+t2.getAge());
        t2.teach();
    }
}

Student case
Specific affairs: Thirteen students, twelve students
Commonness: name, age, class, study, eat

analysis:
    Thirteen students:
        Name, age, class
        Learning (learning) Java Basic), eat
    Twelve students:
        Name, age, class
        Learning (learning) Hive),having dinner
    Abstract participants:
        Name, age, class
        Learning (defined as abstract methods), eating

    realization:
    Thirteen students:
        Name, age, class
        Learning (learning) Java Basic), eat
    Twelve students:
        Name, age, class
        Learning (learning) Hive),having dinner
    Abstract participants:
        Name, age, class
        Learning (defined as abstract methods), eating
abstract class Trainee{
    private String name;
    private int age;
    private String clazz;

    public Trainee() {
    }

    public Trainee(String name, int age, String clazz) {
        this.name = name;
        this.age = age;
        this.clazz = clazz;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    //This is an abstract learning method
    public abstract void study();

    //Specific methods of eating rice
    public void eat(){
        System.out.println("eat rice");
    }
}

class ThirteenTrainee extends Trainee{

    public ThirteenTrainee() {
    }

    public ThirteenTrainee(String name, int age, String clazz) {
        super(name, age, clazz);
    }

    @Override
    public void study() {
        System.out.println("study Java Basics");
    }
}

class TwelveTrainee extends Trainee{

    public TwelveTrainee() {
    }

    public TwelveTrainee(String name, int age, String clazz) {
        super(name, age, clazz);
    }

    @Override
    public void study() {
        System.out.println("study Hive");
    }
}


public class AbstractTest3 {
    public static void main(String[] args) {
        Trainee t1 = new ThirteenTrainee("Chen Junrong",17,"Thirteen issues");
        System.out.println(t1.getName()+"---"+t1.getAge()+"---"+t1.getClazz());
        t1.study();
        t1.eat();

        Trainee t2 = new TwelveTrainee("Navigation",18,"Twelve issues");
        System.out.println(t2.getName()+"---"+t2.getAge()+"---"+t2.getClazz());
        t2.study();
        t2.eat();
    }
}

If we need to design the Employee class when developing a system, the employee includes three attributes: name, job number and salary.
The manager is also an employee. In addition to the employee attribute, there is also a bonus attribute.
Please use the idea of inheritance to design Employee class and manager class. The class is required to provide necessary methods for property access.

    analysis:
        Ordinary employees:
            Name, job number, salary
            Work (code)

        Manager:
            Name, job number, salary, bonus
            Work (do) PPT)

    realization:
        Abstract Employee class
        Ordinary employees
        Manager class
abstract class Staff{
    private String name;
    private String id;
    private int salary;

    public Staff() {
    }

    public Staff(String name, String id, int salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public abstract void work();
}

class GeneralWorker extends Staff{

    public GeneralWorker() {
    }

    public GeneralWorker(String name, String id, int salary) {
        super(name, id, salary);
    }

    @Override
    public void work() {
        System.out.println("Knock code");
    }
}


class Manager extends Staff{

    private int bonus;

    public Manager() {
    }

    public Manager(String name, String id, int salary) {
        super(name, id, salary);
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }

    @Override
    public void work() {
        System.out.println("do PPT");
    }
}

public class AbstractTest4 {
    public static void main(String[] args) {
        Staff s1 = new GeneralWorker("Chen Junrong","0001",14000);
        System.out.println(s1.getName()+"---"+s1.getId()+"---"+s1.getSalary());
        s1.work();

        Staff s2 = new Manager("Little tiger","00080",30000);

        //The downward transformation uses subclass specific methods
//        s2.setBounts();
        ((Manager)s2).setBonus(4000000);
        System.out.println(s2.getName()+"---"+s2.getId()+"---"+s2.getSalary()+"---"+((Manager) s2).getBonus());
        s2.work();


    }
}

Posted by josh_mcqueen on Wed, 29 Sep 2021 16:53:21 -0700