2022-07-26 Zhang Mingxu abstract, interface, stack, queue learning record

catalogue

Feeling

Knowledge points

The fourth feature of object-oriented: abstraction

Abstract method: (no braces, no method body)

The fundamental significance of abstract methods

What does the abstract method stipulate

Abstract methods must be in an abstract class (or interface)

abstract keyword

Summary:

Interface

Declaration of interface

Influence of JDK version change on interface

Characteristics of structure in interface

Significance of interface

Three middle school programming (understand)

Interfaces can be implemented: implementation classes

Interview questions

Interview questions

Anonymous implementation class

Stacks and queues

What are the limitations

Similarities between stack and queue

Differences between stack and queue

Basic operation of stack

Basic operation of queue

Feeling

Today, I learned a lot of things about calling back and forth. This is the last major feature of object-oriented. It's really difficult to clear up the calling in the object-oriented stage. My hair is gone...

Knowledge points

The fourth feature of object-oriented: abstraction


In addition to classes, there are abstract classes and interfaces in Java

Abstract method: (no braces, no method body)


When the parent method needs to be overridden; You can define it directly without outputting anything, but you need to use the abstract keyword.

public abstract void eat();

The fundamental significance of abstract methods


A convention to customize the rules of a sampling method, so as to control subclasses to override the rules of the sampling method

What does the abstract method stipulate


It stipulates the return permission, access permission and parameter list when subclasses rewrite methods, and needs to define the method body when overriding

Abstract methods must be in an abstract class (or interface)

public abstract class Animal {}

1. What structures can there be in an abstract class?

  • attribute

  • Member method

  • constructor

  • Abstract method

  • constant

2. Can there be no abstract methods in abstract classes?

  • Abstract classes can have no abstract methods, but if there are no abstract methods in a class, try not to define it as an abstract class

3. Abstract classes cannot be instantiated:

  • Abstract classes cannot create objects
        /*
          Abstract classes cannot be instantiated = abstract classes cannot create objects
         */
// Error: Animal animal = new Animal();

4. Significance of abstract class constructor

  • In order to contract, the constructor of a subclass must match the parent class

5. If a (non abstract class) subclass inherits an abstract class, all abstract methods in the abstract class must be overridden in the subclass.

For example:

The Animal class is the parent class, including eat () method and show () method. If Person is a subclass, there must be overrides of these two methods in the subclass!!

public abstract class Animal {

    private String name;
    private Integer age;

    public abstract void eat();

    public abstract void show();

    public Animal() {
    }
public class Person extends Animal{

    @Override
    public void eat(){
        System.out.println("People are showing off their food");
    }

    @Override
    public void show() {
        System.out.println(". . . There's nothing to show. Let's split a fork...");
    }

abstract keyword


abstract decoration:

  • class
  • method

  • Can abstract methods be decorated with private---- No, in development, abstract methods are basically public
  • Can abstract methods be decorated with final------ No, the method modified by final cannot be rewritten. The meaning of abstract method is to let subclasses rewrite
  • Can abstract classes be decorated with final-------- No, the class decorated by final cannot be inherited. The meaning of abstract methods is to let subclasses inherit

Summary:


1. Abstract methods must be public or protected. By default, they are public. If they are private, they cannot be inherited by subclasses and cannot be implemented


2. Abstract classes cannot be instantiated directly, and they need to be handled by subclasses in an upward transformation way


3. The significance of the existence of abstract classes: there must be subclasses, and a class can only inherit one (Abstract) class


4. Subclasses (if not abstract classes) must override all abstract methods in the parent abstract class
If subclasses do not fully implement abstract class methods, subclasses should also be defined as abstract classes

Cases: Person, Animal, AnimalSon

  • Define Animal abstract classes;
  • Define the Person class to inherit the Animal class, reflecting some properties of the abstract class.
  • AnimalSon class representation: abstract class inherits abstract class

Case complete code:

Animal class:

public abstract class Animal {

    private String name;
    private Integer age;

    public abstract void eat();

    public abstract void show();

    public Animal() {
    }

    public Animal(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

Person class:

public class Person extends Animal{

    @Override
    public void eat(){
        System.out.println("People are eating");
    }

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

    public void drank() {
        System.out.println("People are drinking");
    }
}

AnimalSon class:

public abstract class AnimalSon extends Animal{
}

Interface

  • More advanced abstraction than abstract classes

Declaration of interface


interface keyword:

  • Used to create interface classes.

Form:

public interface ch02

Influence of JDK version change on interface

  • Before JDK1.7: the interface can only have abstract methods, static constants and static methods, and cannot have attributes and methods
  • JDK8 and later: there are abstract methods, default methods, static methods and static constants in the interface

Characteristics of structure in interface

  • The abstract method in the interface defaults to and is recommended to be omitted

       "public abstract"

  • The constant in the interface defaults to and is recommended to be omitted

       "public static final"

  • The structure in the interface must be

       "public"

Significance of interface

  • Convention: meaning of convention with abstract method

Three middle school programming (understand)

  • object-oriented programming
  • Interface oriented programming: mainly write interfaces (it is difficult to define interfaces according to requirements)
  • Aspect oriented programming (difficult)

Interfaces can be implemented: implementation classes

Implementation class

  • For example, "class A implements interface", let's say that A is the implementation class of this interface.

For example, if Biology class is an interface, the following code Animal is the implementation class (the class that implements the methods in the interface).

public class Animal implements Biology{}

nature

  • If a class implements an interface, it needs to rewrite all the abstract methods in the interface
  • Multiple implementation interfaces can be implemented, and inheritance can only be single
  • In development, if a problem can be solved by inheriting and implementing interfaces, use interfaces to solve it

supplement

Prerequisites for polymorphism?
1. Inheritance. realization
2. Rewrite method
3. Parent class -- subclass; Interface -- "implementation class"

Interview questions


1. Similarities and differences between inheriting abstract classes and implementing interfaces
Difference: single inheritance, multiple interface implementations
Same: all abstract methods need to be rewritten
2. Differences between abstract classes and interfaces
Abstract classes contain many things
Interfaces only have abstract methods, static constants, static methods, and default methods

Interview questions


Only single inheritance in Java?
Wrong, there is multiple inheritance in Java
How much inheritance is there between interfaces

Single inheritance between classes

Case classes: Biology, Cat, CatsAnimal, Animal

Biology class:

public interface Biology {

    // static const 
    public static final String NAME = "Zhang San";
    // Abstract method
    public abstract void breath();
    // Abstract method
    public abstract void eat();
    // Static method
    public static void show(){

    }
    // Default method
    // Who can call it
    public default void info(){

    }
}

Cat class:

/*
  This Cat class inherits the Animal class and implements the Biology interface
  Cat Class can be called a subclass of Animal class
  It can also be called the implementation class of Biology class
*/
public class Cat extends Animal implements Biology,CatsAnimal {

    @Override
    public void breath() {

    }

    @Override
    public void show() {

    }

    @Override
    public void eat() {

    }
}

CatsAnimal class:

public interface CatsAnimal {
}


Anonymous implementation class

  • Form:


Parent object -- nameless implementation class{
Rewritten method
}

  • The interface form is the same as above

Generally, famous implementation classes are used

*/
public class Ch01 {
    public static void main(String[] args) {
        Ch01 ch01 = new Ch01();

        Abstr01 a01 = new Abstr01() {
            //This is an anonymous class, which can have attributes, methods, etc
            @Override
            public void show() {
                System.out.println("Rewritten show method");
            }
        };
        a01.show();
        System.out.println(a01);
        Inter01 inter01 = new Inter01() {
            @Override
            public void eat() {
                System.out.println("Rewritten eat method");
            }
        };
        //Well known implementation classes
        Abstr01 a02 = new Ch02();
    }
}


Stacks and queues

  • Stack and queue are two linear tables with limited operations

What are the limitations

  • Stack insertion and deletion are only allowed at the end of the table (called "top of stack" in the stack), which meets FILO (first in last out)
  • The queue only allows inserting elements in the table position, deleting elements in the header, FIFO (first in first out)

Similarities between stack and queue


1. All linear structures
2. The insertion operation is carried out at the end of the table
3. Both can be realized through sequential structure and chain structure
(suppose yesterday's one-way linked list, which I wrote in my last blog: superlinked, is used to form a stack and a queue to complete some basic operations of the stack and queue.)

Differences between stack and queue

  • Stack: in and out;
  • Queue: first in, first out



Basic operation of stack

public class Stack {
    private SuperLinked superLinked = new SuperLinked();
    //Stack up
    public void push(Integer i){
        superLinked.add(i);
    }

    //Return to the top of the stack element, no stack
    public Integer peek(){
        if (empty()){
            return null;
        }
        return superLinked.get(superLinked.size() - 1);
    }

    //Get out of the stack, get out from the end of the stack
    public boolean pop(){
        if (empty() == false){
            return superLinked.removeLast();
        }
        return false;
    }

    private boolean empty(){
        return superLinked.size() == 0;
    }

    public static void main(String[] args) {
        // test
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);

        stack.pop();

        System.out.println(stack.peek());
    }

Basic operation of queue

public class Queue {
    private SuperLinked superLinked = new SuperLinked();
    //How to join the team
    public void add(Integer item){
        superLinked.add(item);
    }

    //Method of leaving the team

    public Integer poll(){
        //1. Judge whether the queue is empty
        if (empty()){
            return null;
        }

        //2. Find the head of the queue
       Integer integer  =  superLinked.get(0);
        //3. Delete the head of the team
        superLinked.removeHead();
        //4. Return the deleted value
        return integer;
    }

    //Return to the head of the team, not out of the team
    public Integer peek(){
        if (empty()){
            return null;
        }
        return superLinked.get(0);
    }

    //Determine whether this queue is empty
    public boolean empty(){
//        if (superLinked.size() == 0){
//            return true;
//        }else{
//            return false;
//        }
       return superLinked.size() == 0;
    }

    public static void main(String[] args) {
        Queue queue = new Queue();
        queue.add(1);
        queue.add(120);
        queue.add(50);

        queue.poll();

        System.out.println(queue.peek());
    }
}

Tags: Java

Posted by Julian on Fri, 29 Jul 2022 02:32:39 +0930