Abstract class of Java object oriented

Time:2021-4-13

Java abstract class

 

  1. Java abstract class:

    1. Abstract modifier can be used to modify a method or a class. If you modify a method, the method is an abstract method; if you modify a class, the class is an abstract class

    2. Abstract classes may not have abstract methods, but classes with abstract methods must be declared as abstract classes

    3. An abstract class cannot use the new keyword to create an object. It is used for subclasses to inherit

    4. Abstract methods have only method declaration, but no method implementation. They are used to make subclasses implement

    5. Subclass inherits the abstract class, so we must implement the abstract method that the abstract class does not implement, otherwise the subclass must also be declared as an abstract class

    be careful:

    1. You can’t use the abstract class new, you can only rely on subclasses to implement it, and you can’t use constraints

    2. Ordinary methods can be written in abstract classes

    3. Abstract methods must be in abstract classes

    Extension:

    1. Abstraction of abstraction: Constraints

    2. Class: Extensions single inheritance interface can inherit multiple

     

  2. example:
    package oop.demo12;
    
    /**
     *Abstract class:
     *1. The abstract modifier can be used to modify methods or classes,
     *If you modify a method, it is an abstract method;
     *If you modify a class, it is an abstract class
     *2. There can be no abstract methods in the abstract class, but the class with abstract methods must be declared as an abstract class
     *3. An abstract class cannot use the new keyword to create an object. It is used for subclasses to inherit
     *4. The abstract method has only the declaration of the method, but no method implementation. It is used to make the subclass implement
     *5. Subclass inherits the abstract class, so we must implement the abstract method that the abstract class does not implement, otherwise the subclass should also be declared as an abstract class
     *Note:
     *1. You can't use the abstract class new, you can only rely on subclasses to implement it, and you can't use constraints
     *2. Ordinary methods can be written in abstract classes
     *3. Abstract methods must be in abstract classes
     *Extension:
     *1. Abstract abstraction: Constraints
     *2. Class: Extensions single inheritance interface can inherit multiple
     */
    public abstract class Action {
    
        /**
         *Abstract method
         *Only method name
         *There is no implementation of the method
         */
        public abstract void doSomething();
    }

Inherit abstract classes

 

  1. Subclasses inherit abstract classes

    All methods of an abstract class must have subclass implementations

    All subclasses that inherit it must implement its methods

    Unless the class is also abstract

  2. example:
    package oop.demo12;
    
    /**
     *All methods of an abstract class must have subclass implementations
     *All subclasses that inherit it must implement its methods
     *Unless the class is also abstract
     */
    public class A extends Action {
    
        //Inheriting abstract classes override methods in abstract classes
        @Override
        public void doSomething() {
            System.out.println("Override");
        }
    
    }
     

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]