Java object oriented

Note: this section is used to review abstractions and interfaces.

1, abstract

① A method modified by the abstract keyword is called an abstract method, which has no method body;

② The class modified by the abstract keyword is an abstract class. If a class contains abstract methods, the class must be an abstract class;

③ When a subclass inherits the abstract parent class, there are two solutions:

  • Scheme 1: become an abstract subclass, and the subclass will not be implemented. Continue to abstract;

  • Scheme 2: become an ordinary subclass and implement all abstract methods in the abstract parent class.

④ Abstract classes cannot create objects (instantiation).

⑤ Abstract classes have constructors, but they are not used by themselves for the use of subclasses super();

⑥ Abstract classes can define member variables and member constants;

⑦ Abstract classes can be all ordinary methods, all abstract methods, or semi general and semi abstract;

⑧ If a class does not want to be instantiated by the outside world, it can be declared as an abstract class.

1.2abstract precautions

Abstract methods require subclasses to be overridden. When abstract is used with the following keywords, it is meaningless in abstract classes:

  • private: after being privatized, subclasses cannot be rewritten, which is contrary to abstract;
  • Static: static takes precedence over the object, and there is a problem with the loading order;
  • Final: it cannot be rewritten after being modified by final, which is contrary to abstract.
/**This class is used as an introductory case for abstract classes*/
public class AbstractDemo {
    public static void main(String[] args) {
        /**4.Abstract classes cannot create objects (instantiation);
         * */
       // Animal a1 = new Animal();
        Animal a2 = new Pig();
        a2.eat();
        a2.fly();
        a2.fly2();
    }
}
/**2.The class modified by the abstract keyword is an abstract class. If
 * If a class contains abstract methods, the class must be declared as an abstract class*/
abstract class Animal{
    public void eat(){
        System.out.println("You can eat anything~");
    }
    public void play(){
        System.out.println("You can play anything~~");
    }
    /**1.A method modified by the abstract keyword is called an abstract method. An abstract method has no method body*/
    public abstract void fly();
    public abstract void fly2();
}
/**3.When a subclass inherits the abstract parent class, there are two solutions:
 * Scheme 1: become an abstract subclass, and the subclass will not be implemented. Continue to abstract
 * Scheme 2: become an ordinary subclass and implement all abstract methods in the abstract parent class*/
//abstract class Pig Animal() {} --- scheme 1

class Pig extends Animal{
    @Override
    public void fly() {
        System.out.println("Can pay off~~take off~");
    }

    @Override
    public void fly2() {
        System.out.println("All abstract methods in the abstract parent class need to be implemented~!");
    }
}

/***********************************************************/
**This class is used to test construction methods in abstract classes
 * 1.Abstract classes have construction methods;
 * 2.Abstract classes cannot create objects and have construction methods. They are not used when creating objects themselves, but when creating objects for subclasses super()
 * */
public class AbstractDemo2 {
    public static void main(String[] args) {
       // Animal2 a = new Animal2();
        Pig2 p = new Pig2();
    }
}
/**1.Member variables can be defined in abstract classes;
 * 2.Member constants can be defined in abstract classes;
 * 3.Ordinary methods can be defined in abstract classes;
 * 4.Abstract classes can all be ordinary methods;
 * 5.If an abstract class is an ordinary method, why should it be declared as an abstract class?
 * Because abstract classes cannot be instantiated, if you don't want the outside world to create objects of this class, you can declare ordinary classes as abstract classes
 *6.Abstract methods can be defined in abstract classes, or they can all be abstract methods
 *  */
abstract class Animal2{

    public Animal2(){
        System.out.println("This is the parametric constructor of an abstract class");
    }
}
/**An abstract class cannot create an object. An abstract class has a constructor*/
class Pig2 extends Animal2{
    public Pig2(){
        super();//Call the parameterless construction of the parent class
        System.out.println("This is Pig2 Construction method of~");
    }
}

2, Interface

Interface is an abstract type in Java. The content of the interface is an abstract function that needs to be implemented. The interface is more like a rule and a set of standards.

Interface characteristics:

  • Define the interface through the interface, and let the subclass implement the interface through the implements;
  • There is no constructor in the interface;
  • If a class does not explicitly specify its own parent class, it inherits the top-level parent class Object by default;
  • There are no member variables in the interface. Some are static constants, which will be spliced into public static final by default   int   n = XX;
  • The abstract methods in the interface can be abbreviated, and public abstract will be spliced by default.
  • The interface can be understood as a special abstract class, but the interface is not a class!!!
  • The description of a class is the attribute and method of a class of things, and the interface is the method to be implemented by the implementation class;
  • Interfaces and classes can be implemented and interfaces can inherit from each other;
  • The interface is an external exposure rule and a set of development specifications;
  • The interface improves the expansibility of the program and reduces the coupling;
/**This class is used to test interface oriented programming*/
public class TestTeacherInter {
    public static void main(String[] args) {
        CGBTeacher ct = new CGBTeacher();
        ct.ready();
        ct.play();
        ct.teach();
    }
}
//Create a teacher interface. Is the interface the result of innate design? The first design is the interface, and rules should be formulated
interface Teacher{
    void teach();//public abstract void teach()
    void play();//public abstract void play()
    void ready();
}
class CGBTeacher implements Teacher {
    @Override
    public void teach() {
        System.out.println("Teaching e-commerce project~");
    }
    @Override
    public void play() {
        System.out.println("Lesson preparation e-commerce project~");
    }
    @Override
    public void ready() {
    }
}
class SCDTeacher implements Teacher{
    @Override
    public void teach() {
    }
    @Override
    public void play() {
    }
    @Override
    public void ready() {
        System.out.println("Preparing lessons....");
    }
}

/******************************************************************/
/**This class is used to test members in an interface*/
public  class TestUserInter2 {
    public static void main(String[] args) {
        UserInter2Impl i2 = new UserInter2Impl();
        System.out.println(i2.n);
        System.out.println(i2.NAME);
        System.out.println(UserInter2.NAME);
        //UserInter2.n = 40;// The value of a constant cannot be modified
        /**3.Abstract methods can be defined in interfaces*/
        /**4.The abstract method in the interface can be abbreviated when defined, and the method will be spliced with public abstract automatically*/
    }
}
interface UserInter2{
    /**1.Member variables cannot be defined in an interface
     * 2.Static constants in the interface are actually written as follows: public static final int age = 20;
     * However, the interface can be omitted and not written. It will be spliced by default. We write it as int age = 20; it's fine too
     * */
    public static final int n = 100;
    final String NAME = "swx";
}
//Create an implementation class for the interface
class UserInter2Impl implements UserInter2{
}

Tags: Java Back-end

Posted on Sun, 05 Dec 2021 04:39:54 -0500 by ClyssaN