Java object oriented -- abstract classes and interfaces

1, Abstract class

1. Origin of abstract classes

In a class with inheritance relationship, the method of overriding the parent class by a subclass can be written or not. If it is not written, the method of the parent class will be looked up when creating a subclass object.

If you need to force subclasses to override parent methods, you can use abstract classes.

2. Definition of abstract class

This class is conceptualized. There is no way to describe the common properties and behavior of this class of objects in a specific instance.

For example: human beings have many races. Their attributes are similar and their behaviors are similar, but there is no way to concretize them to one point. This is abstract.

Abstract classes are upgraded versions of ordinary classes, which have more abstract methods than ordinary classes.

3. Provisions on abstract classes

  1. The class of the abstract method must be an abstract class [0... N]
  2. If a subclass inherits an abstract class, it must override all abstract methods. The subclass here is an ordinary class. If the subclass is also an abstract class, you can optionally override the abstract method
  3. Abstract keyword is used to define abstract classes or abstract methods in Java
  4. An abstract class cannot instantiate an object directly through this class. This class is an abstract concept, which can only be transformed upward through a subclass into a reference to an abstract parent class
//Explain the fourth point
public abstract class Sharp {
    public abstract void print();
}
class Test{
    public static void main(String[] args) {
        Sharp sharp = new Sharp();//error
    }
}
//report errors
java: interface_test.Sharp It's abstract; Cannot instantiate
//Third point
abstract class A {
    abstract void printA();
}
// B is an abstract class, which can optionally override the abstract methods of the parent class
abstract class B extends A{
    abstract void printB();
}
// C is a normal class and must override all abstract methods in B (including inherited abstract methods)
//c needs to override all the abstract methods of the parent class, so here we need to write two
public class C extends B{
    void printB() {}
    void printA() {}
}

If class B overrides the abstract method of class A in the above code, class C only needs to write the abstract method of class B. the code is as follows

abstract class A {
    abstract void printA();
}
// B is an abstract class, which can optionally override the abstract methods of the parent class
abstract class B extends A{
    abstract void printB();
    void printA(){}
}
public class C extends B{
    void printB() {}
}

4. Definition of abstract method

Using abstract keyword declaration, only function declaration and no method body are called abstract methods.

//abstract class
public abstract class Sharp {
    //Abstract method
    public abstract void print();
}

5. Characteristics of abstract classes

  1. Abstract classes are supersets of ordinary classes. There are some ordinary classes and all abstract classes, but there are some more abstract methods than ordinary classes.

  2. Abstract classes cannot instantiate objects directly, but there can be constructors.

  3. When a subclass is instantiated, it follows the principle of inheritance, calling the constructor of the parent class (abstract class) first, and then the constructor of the subclass

abstract class BaseTest {
    //Construction method of abstract class
    public BaseTest() {  
        this.print();  //First step
    }
    abstract void print();
}
public class Fun extends BaseTest{
    private int num = 10;
    void print() {
        System.out.println("num = " + num); //Step two
    }
    public static void main(String[] args) {
        new Fun();
    }
}
Output:
num = 0
  1. Create an object of Fun. Because Fun inherits BaseTest, call the constructor of the parent class first
  2. The construction method of the parent class calls the print method. Because the subclass overrides this method and new is the object of the subclass, the print method of the subclass is called
  3. At this time, initialization has not been completed, and it is still the default value, so num = 0 is output

2, Interface

1. Use of abstract classes and interfaces

Summary: when a class can use both abstract classes and interfaces. Interfaces are preferred because abstract classes are inherited only and have limitations.

Use of interface:

  1. The interface indicates that it has certain ability / behavior. The subclass implements the interface not because of is a, but because it has such behavior or ability

    For example, people, ducks and dogs have the ability to eat, and they can all implement this interface. But man is not a dog!!!!

  2. An interface represents a specification or standard

    For example, USB interface (a standard), everything that meets this interface can be inserted.

2. Use of interface representation standards

The interface only has global constants and abstract methods, and nothing else

The interface uses the keyword interface to declare the interface, and the subclass uses implementation to implement the interface

Example: realize USB interface

  1. Declare USB interface
public interface USB {
    // insert
    public abstract void plugIn();
    // work
    public abstract void work();
}
  1. Create a subclass of USB
//mouse
class Mouse implements USB{
    @Override
    public void plugIn() {
        System.out.println("Install mouse driver~");
    }

    @Override
    public void work() {
        System.out.println("The mouse starts working normally~");
    }
}
//keyboard
class KeyBoard implements USB{
    @Override
    public void plugIn() {
        System.out.println("Installing keyboard driver~");
    }

    @Override
    public void work() {
        System.out.println("The keyboard is working properly~");
    }
}
//camera
 class Camera implements USB{
    @Override
    public void plugIn() {
        System.out.println("Medium drive camera installation~");
    }

    @Override
    public void work() {
        System.out.println("The camera is working properly~");
    }
}
  1. Create a computer class. It is not a subclass of USB, but the carrier and user of USB
public class Computer{
    public static void main(String[] args) {
        Computer computer = new Computer();
        Mouse mouse = new Mouse();
        // Insert mouse 
        computer.fun(mouse);
        KeyBoard keyBoard = new KeyBoard();
        // Insert keyboard 
        computer.fun(keyBoard);
        Camera camera = new Camera();
        computer.fun(camera);
    }
    //All subclasses of USB class are received here. As long as the USB interface is met, they can be inserted into the computer. It doesn't care which device is specific. As long as the interface is met, the computer can be used. It reflects polymorphism
    //Compatible with all USB subclass objects
    //Upward transformation
    public void fun(USB usb) {
        //Call two methods overridden by subclasses
        usb.plugIn();
        usb.work();
    }
}

4. Output

Install mouse driver~
The mouse starts working normally~
Installing keyboard driver~
The keyboard is working properly~
Installing camera driver~
The camera is working properly~

3. Use of interface presentation capability

Interfaces allow multiple implementations. A class can have multiple capabilities and implement multiple parent interfaces at the same time. If multiple parent interfaces are implemented, the subclass is a common class and needs to override all abstract methods

In the interface proof, you don't need to write public abstract keywords. You just need to keep the method return value, method parameter list and name

  1. Ability to swim - realize swimming interface
public interface ISWim {
    //public abstract void swim();
    void swim();
}
  1. Ability to run -- realize running interface
public interface IRun {
    void run();
}
  1. Ability to fly - realize flight interface
public interface IFly {
    void fly();
}
  1. Create a subclass, rabbit - only the running interface can be implemented
public class Rabbit implements IRun{
    @Override
    public void run() {
        System.out.println("The rabbit is running~~~");
    }
}
  1. Create dog subclass -- implement running and swimming interfaces
public class Dog implements IRun,ISWim{
    @Override
    public void run() {
        System.out.println("The dog is running~~~");
    }

    @Override
    public void swim() {
        System.out.println("The dog is digging~~~");
    }
}
  1. Create a duck class - realize the interface of running, swimming and flying
public class Duck implements IRun,ISWim,IFly{
    @Override
    public void fly() {
        System.out.println("Ducks are flying~~~");
    }

    @Override
    public void run() {
        System.out.println("The duck is running~~~");
    }

    @Override
    public void swim() {
        System.out.println("The duck is swimming~~~");
    }
}
  1. Test class
public class Test {
    public static void main(String[] args) {
        // The interface cannot directly instantiate objects, and needs to be transformed upward
        IRun run = new Rabbit();
        IRun run1 = new Dog();
        ISWim sWim = new Dog();
        IFly fly = new Duck();
        run.run();
        run1.run();
        sWim.swim();
        fly.fly();
    }
}
Output:
The rabbit is running~~~
The dog is running~~~
The dog is digging~~~
Ducks are flying~~~

Tags: Java

Posted by itpvision on Mon, 02 May 2022 15:45:16 +0300