简体   繁体   中英

Interface methods java abstract

Is it true that an interface's methods are all abstract so you must override them? Thanks. I can't remember if the interfaces are all abstract and require override. Thank you.

Leaving out the new stuff introduced in Java 8 which probably only complicates your question, yes all interface methods are abstract. If you have an interface like this...

public interface SomeInterface {
    public void methodOne();
    public void methodTwo();
}

Then a class like this will not compile.

public class SomeClass implements SomeInterface {

}

The code will not compile because SomeClass does not provide an implementation for all of the methods which are defined in SomeInterface.

The following class will compile because it provides implementatsion for all of the interface methods.

public class SomeClass implements SomeInterface {
    public void methodOne() {
        // ...
    }

    public void methodTwo() {
        // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM