java abstract (abstract)

What is abstract?

As the name suggests: very unspecific, no specific implementation, just a concept

What can abstarct modify?

  1. Modified class: The class modified with abstract modifier is called abstract class
  2. Modification method: The method modified with the abstract modifier only has the structure of the method (end with ;), and there is no method execution body called abstract method.
    Note that although the native modified method does not have a method body, it is not an abstract method, but the execution process is other Written in language (call local C/C++ language)

What are the characteristics of abstarct modification?

  1. Abstract classes do not have to contain abstract methods, they can all be concrete
  2. If the method is modified with abstract, the class must be an abstract modified class (when I am also an interface implements), ordinary classes are not allowed to contain abstract methods.
  3. Abstract classes can directly inherit abstract classes
  4. Abstract classes can directly inherit concrete classes (but generally not written like this)
  5. The concrete class cannot directly inherit the abstract class, and the abstract method in the parent class needs to be reified (or the subclass also becomes an abstract class, waiting for the next concrete class to be reified)

What is the difference between abstract class and ordinary class?

  1. In addition to allowing abstract methods, abstract classes are basically the same as ordinary classes
  2. An abstract class contains a construction method, but you cannot directly create an object by calling the construction method. It can only do things through subclass (ordinary class) inheritance. By calling the subclass construction method, the default call super(); (that is, the parent class construction method is called silently ) Create a parent abstract class object to do things
  3. You can also create abstract class objects through anonymous inner classes, provided that there are abstract methods in the abstract class, and the abstract methods in the abstract class are concretized through anonymous classes.

Test example:
Animal abstract class

public abstract class Animal {
    
    

    public Animal(){
    
    }


    public abstract void eat();


    public void sleep(){
    
    
        System.out.println("sleep方法。。。");
    }
}

Subclass Person inherits the Animal abstract class

public class Person extends Animal{
    
    

    @Override
    public void eat() {
    
    
        System.out.println("eat方法。。。。");
    }
}

Test category:

public class TestMain {
    
    
    public static void main(String[] args) {
    
    
        //通过匿名内部类创建抽象类对象
        Animal animal = new Animal() {
    
    
            @Override
            public void eat() {
    
    
                System.out.println("匿名方法。。。");
            }
        };
        //执行是匿名类重写父类中的抽象的方法
        animal.eat();
        //Animal中普通的方法
        animal.sleep();

        //创建子类的对象
        Person person = new Person();
        //可以使用父类独有的方法
        person.sleep();
        //执行的是子类中重写父类中抽象的方法
        person.eat();


    }
}

Test Results:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45608165/article/details/112918666