编程知识 cdmana.com

Java interface

{tabs-pane label="interface"}

interface Pure abstract interfaces that are more abstract than abstract classes , Because it can't even have fields . Because all methods defined by the interface default to public abstract Of , So these two modifiers don't need to be written ( The effect is the same whether you write or not ).

// Comparison of abstract class methods 
abstract class demo4_1{
    public abstract void run();
    public abstract String name();
}
interface demo4_1{
    void run();
    String getName();
}
interface demo4_1{
    void run();
    String getName();
}
interface demo4_1chou{
    void run();
    String name();
}
 //class Realization interface Need to use implements keyword 
class demo4_1_1 implements demo4_1{
    private String name;
    public demo4_1_1(String name){// Construction method 
        this.name=name;
    }
    @Override    // Inherit 
    public void run() {// Methods in the parent class 
        System.out.println(this.name+" 1");
    }
    @Override
    public String getName() {// Fields in parent class 
        // TODO Auto-generated method stub
        return this.name;
    }
}

One class can only inherit from another class , Cannot inherit from more than one class . however , A class can implement multiple interface, for example :

class demo4_1_2 implements demo4_1,demo4_1chou{
    protected String name;
    @Override
    public void run() {}
    @Override
    public String getName() {
        return this.name;}
    @Override
    public String name() {
        return this.name;}
}

{/tabs-pane} {tabs-pane label=" The term "}

The term Pay attention to distinguishing terms : Java The interface of refers specifically to interface The definition of , Represents an interface type and a set of method signatures , And programming interface generally refers to interface specification , Such as method signature , data format , Network protocol, etc .

abstract class

interface

Inherit

Can only extends One class

Sure implements Multiple interface

Field

You can define instance fields

Cannot define instance fields

Abstract method

You can define abstract methods

You can define abstract methods

Nonabstract method

You can define non abstract methods

Can define default Method

{/tabs-pane} {tabs-pane label=" Interface inheritance extends"}

One interface Can inherit from another interface.interface Inherited from interface Use extends, It is equivalent to a method that extends the interface

// Interface inheritance  extends
interface demo4_1_3 extends demo4_1{
    void run();
    String getName();
}

{/tabs-pane} {tabs-pane label="default Method "}

Implementation classes can be overridden default Method .default The purpose of the method is , When we need to add a method to the interface , It involves modifying all subclasses . If what's new is default Method , So subclasses don't have to be completely modified , Only need to overwrite the new method where it needs to be overwritten .

default Methods are different from ordinary methods of abstract classes . because interface No fields ,default Method cannot access field , Ordinary methods of abstract classes can access instance fields .

demo4_1 d=new demo4_1_4(" I'm the default method ");
        d.run();
interface demo4_1{
    //void run();
    String getName();
     default void run(){System.out.println(this.getName()+" run");}
}
class demo4_1_4 implements demo4_1{
    private String name;
    public demo4_1_4(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
}

{/tabs-pane}

This article altogether 425 Number of words , Average reading time ≈ 2 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824193i.html

Scroll to Top