[Yugong series] December 2021 Java Teaching Course 32 - abstract classes

1, Abstract class

1. Overview of abstract classes (understanding)

When we are doing subclass common function extraction, some methods are not embodied in the parent class. At this time, we need to abstract classes!

In Java, a method without a method body should be defined as an abstract method, and if there is an abstract method in a class, the class must be defined as an abstract class!

2. Characteristics of abstract classes (memory)

  • Abstract classes and methods must be decorated with the abstract keyword

    //Definition of abstract class
    public abstract class Class name {}
    
    //Definition of abstract methods
    public abstract void eat();
    
  • Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes

  • Abstract classes cannot be instantiated

  • Abstract classes can have constructors

  • Subclasses of abstract classes

    Or override all abstract methods in an abstract class

    Or it's an abstract class

3. Cases of abstract classes (Applications)

  • Case requirements

    Define cat and dog

    Cat member method: eat (cat eats fish) drink (drink water...)

    Dog member method: eat (dog eats meat) drink (drink water...)

  • Implementation steps

    1. There are common contents between cats and dogs, so an Animal should be extracted upward
    2. In the parent class Animal, the concrete implementation of eat method cannot be described clearly, so it is defined as an abstract method
    3. Abstract methods need to survive in abstract classes and define Animal as an abstract class
    4. Let Cat and Dog inherit Animal respectively and override eat method
    5. Create Cat and Dog objects in the test class and call the method test
  • code implementation

    • Animals
    public abstract class Animal {
        public void drink(){
            System.out.println("drink water");
        }
    
        public Animal(){
    
        }
    
        public abstract void eat();
    }
    
    • Cats
    public class Cat extends Animal {
        @Override
        public void eat() {
            System.out.println("Cats eat fish");
        }
    }
    
    • Dogs
    public class Dog extends Animal {
        @Override
        public void eat() {
            System.out.println("Dogs eat meat");
        }
    }
    
    • Test class
    public static void main(String[] args) {
            Dog d = new Dog();
            d.eat();
            d.drink();
    
            Cat c = new Cat();
            c.drink();
            c.eat();
    
            //Animal a = new Animal();
            //a.eat();
        }
    

4. Formwork design mode

  • Design pattern

    Design pattern is a set of code design experience that is repeatedly used, known by most people, classified and catalogued.
    The purpose of using design patterns is to reuse code, make the code easier to be understood by others, ensure code reliability and program reusability.

  • Template design mode

    The abstract class as a whole can be seen as a template, and the things that cannot be determined in the template are defined as abstract methods
    Let classes that use templates (classes that inherit abstract classes) override abstract methods to implement requirements

  • Advantages of template design mode

    The template has defined a general structure, and users only need to care about the functions they need to implement

  • Sample code

    template class

    /*
        Composition template class
     */
    public abstract class CompositionTemplate {
    
        public final void write(){
            System.out.println("<<My father>>");
    
            body();
    
            System.out.println("ah~ This is my father");
    
        }
    
        public abstract void body();
    }
    

    Implementation class A

    public class Tom extends CompositionTemplate {
    
        @Override
        public void body() {
            System.out.println("It was autumn, The wind is so lingering,In memory, " +
                    "That day my father took me home from school by bike,My foot got stuck in the bicycle chain, Dad can't pedal,He got up and kicked...");
        }
    }
    

    Implementation class B

    public class Tony extends CompositionTemplate {
        @Override
        public void body() {
    
        }
    
        /*public void write(){
    
        }*/
    }
    

    Test class

    public class Test {
        public static void main(String[] args) {
            Tom t = new Tom();
            t.write();
        }
    }
    

5.final (application)

  • Function of fianl keyword

    • Final stands for the final meaning. It can modify member methods, member variables and classes
  • final modifies the effects of classes, methods, and variables

    • fianl decorated class: this class cannot be inherited (it cannot have subclasses, but it can have parent classes)

    • final modifier method: this method cannot be overridden

    • final modifier variable: indicates that the variable is a constant and cannot be assigned again

      • Variables are basic types, and values cannot be changed

      • Variables are reference types. What cannot be changed is the address value, but the contents of the address can be changed

      • give an example

        public static void main(String[] args){
            final Student s = new Student(23);
          	s = new Student(24);  // error
         	s.setAge(24);  // correct
        }
        

Keywords: Java Back-end

Added by milsaw on Mon, 03 Jan 2022 09:54:24 +0200