Home  >  Article  >  Java  >  Detailed analysis of JAVA interfaces and abstract classes

Detailed analysis of JAVA interfaces and abstract classes

WBOY
WBOYforward
2022-07-08 14:42:251655browse

This article brings you relevant knowledge about java, which mainly sorts out issues related to interfaces and abstract classes. When you need to derive a class from several classes, inherit all of them The properties and methods of JAVA do not have multiple inheritance and need to be implemented with interfaces. Let’s take a look at them together. I hope it will be helpful to everyone.

Detailed analysis of JAVA interfaces and abstract classes

Recommended study: "java video tutorial"

Interface

Overview

  1. When you need to derive a class from several classes, Inheritall their properties and methods, JAVA does not have multiple inheritance and needs to use interface implementation
  2. Extract some from several classes Common behavioral characteristics, and there is no is-a relationship between them, they also need to use interfaces to achieve
  3. The essence of interfaces is contracts and specifications

Use

  1. Use interface in the interface to define
  2. Interface and class are two different structures
  3. In the interface Cannot define a constructor (that is, it cannot be instantiated)
  4. Interfaces are implemented through classes
  5. JAVA classes can implement multiple interface formats: class AA extends BB interface CC, DD
  6. Interfaces can be inherited from each other, and multi-inheritance is possible
  7. The interface embodies polymorphism
public class UsbTest {
    public static void main(String[] args) {
        Computer com =new Computer();
        Flash flash=new Flash();
        com.transferData(flash); // USB usb =new Flash()
        com.transferData(new Printer());
        /*
		* U盘开始工作
		* 传输数据
		* U盘结束工作
		* 打印机开始工作
		* 传输数据
		* 打印机结束工作
		* 
		* */
    }}class Computer{
    public void transferData(USB usb){
        usb.start();
        System.out.println("传输数据");
        usb.stop();
    }}interface USB{
    void start();
    void stop();}class Flash implements USB{

    @Override
    public void start() {
        System.out.println("U盘开始工作");
    }

    @Override
    public void stop() {
        System.out.println("U盘结束工作");

    }}class Printer implements USB{

    @Override
    public void start() {
        System.out.println("打印机开始工作");

    }

    @Override
    public void stop() {
        System.out.println("打印机结束工作");

    }}
  1. How to write JDK8
  • The static method defined in the interface can only be called through the interface
  • through the implementation class Object can implement the default method in the interface
  • If the parent class inherited by the subclass (or implementation class) and the implemented interface declare a method with the same name and the same parameters, if the subclass does not override the method, the called It is a method with the same name and parameters in the parent class
public class JDK8Test {
    public static void main(String[] args) {
        SubClass sub =new SubClass();
        CompareA.methods1();
        sub.methods2();
        /*
        * Compare 北京
		* Compare 上海
        */
    }}class SubClass implements CompareA{}interface CompareA{
    public static void methods1() {
        System.out.println("Compare 北京");
    }
    public default void methods2() {
        System.out.println("Compare 上海");
    }}

Agent mode

  1. The proxy mode is to provide a proxy for other objects to control access to this object
  2. Example
public class NetWorkTest {
    public static void main(String[] args) {
        Server server=new Server();
        ProxyServer proxyServer=new ProxyServer(server); // 放的是被代理对象进去
        proxyServer.browse(); // 调用的是代理对象,但结果是真实对象的方法被调用
        /*
        * 检查工作
		* 真实服务器访问网络
        */
    }}interface NetWork{
    void browse();}// 被代理类class Server implements NetWork{

    @Override
    public void browse() {
        System.out.println("真实服务器访问网络");
    }}// 代理类class ProxyServer implements NetWork{
    private NetWork work;
    public ProxyServer(NetWork work){
        this.work=work;
    }
    public void check(){
        System.out.println("检查工作");
    }

    @Override
    public void browse() {
        check();
        work.browse();
    }}

Internal class

  1. JAVA allows a class A to be declared in another class B, then class A is an internal class and class B is an external class
  2. Internal class classification
  • Member internal class
    • static
    • non-static
  • Local inner class
    • Inside method
    • Inside code block
    • Inside constructor
  1. Instance Convert inner class objects
public class InnerClassTest {
    public static void main(String[] args) {
        // 实例化Dog,静态类
        Pseson.Dog dog=new Pseson.Dog();
        dog.eat();
        // 吃骨头
        // 实例化Cat,非静态类
        Pseson p1 =new Pseson();
        Pseson.Cat cat=p1.new Cat();
        cat.eat();
        // 猫吃鱼
    }}class Pseson{
    String name;
    int age;
    public void eat(){
        System.out.println("吃饭");
    }
    static class Dog{
        String name;
        int age;
        public void eat(){
            System.out.println("吃骨头");
        }
    }
    class Cat{
        public void eat(){
            System.out.println("猫吃鱼");
        }
    }}
  1. Call external class structure
public class InnerClassTest {
    public static void main(String[] args) {
        // 实例化Cat,非静态类
        Pseson p1 =new Pseson();
        Pseson.Cat cat=p1.new Cat();
        cat.eat();
        cat.display("小花");
        /*
        * 小花
        * 波斯猫
        * 人
        */ 
    }}class Pseson{
    String name="人";
    int age;
    public void eat(){
        System.out.println("吃饭");
    }
    static class Dog{
        String name;
        int age;
        public void eat(){
            System.out.println("吃骨头");
        }
    }
    class Cat{
        String name="波斯猫";
        public void eat(){
            System.out.println("猫吃鱼");
        }
        public void display(String name){
            System.out.println(name); // 方法中name
            System.out.println(this.name); // 内部类中name
            System.out.println(Pseson.this.name); // 外部类中name

        }
    }}
  1. Interface and abstract class
  • Similarities: cannot be instantiated; both contain abstract methods; can be inherited
  • Differences: abstract classes have constructors, interfaces have no constructors; abstract classes only have single inheritance, while interfaces can have multiple inheritance; abstract classes extends Inheritance, interface implements implementation

Recommended learning: "java video tutorial"

The above is the detailed content of Detailed analysis of JAVA interfaces and abstract classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete