简体   繁体   中英

What's the difference between public method and abstract method in Interface?

Consider the following interface :

public interface MyInterface {

    public void func1();
    public void func2();
    abstract public void func3();

}

and the class MyClass :

public class MyClass implements MyInterface{

    @Override
    public void func1() {
        // TODO Auto-generated method stub

    }

    @Override
    public void func2() {
        // TODO Auto-generated method stub

    }

    @Override
    public void func3() {
        // TODO Auto-generated method stub

    }
}

What's the difference between func2() and func3() ?

I must implement both when I want to implement MyClass , so it seems that there's no difference if I write public or abstract public in the interface .

Thanks

Java 7 and earlier:

There is no difference since all interface methods are public and "abstract." This is implied whether declared or not.

Java 8:

The same rules apply as in Java 7, however, it should be noted that since Java 8, only non-default methods are "abstract." Default methods , are in fact, allowed to have an implementation.

Java 9:

In Java 9, we are provided even more flexibility, and also allowed to have private methods .

Java Language Specification quote :

Every method declaration in the body of an interface is implicitly abstract , so its body is always represented by a semicolon, not a block.

You ask

What's the difference between func2() and func3() ?

Except for the fact that they are different methods, their modifiers are the same.

What's the difference between public method and abstract method in Interface?

9.1.1.1. abstract Interfaces Simply said... none. According to this, it's obsolete.

In a way, we are comparing apples and oranges in this question. For someone new to the Java language, some clarifications need to be made first:

Interfaces:

  • Interfaces contain method stubs. These are methods without a body. These methods are implicitly "abstract" and we do not need to explicitly use a modifier to denote this.
  • That being said, since Java 8 , we now have what are called " default methods ." These methods are fully implemented methods which do require an explicit declaration using the default keyword.

  • That being said, the abstract keyword never comes into play in the context of an interface.

Abstract Methods:

  • Abstract methods only belong in abstract classes.

  • An abstract class may contain fully implemented methods along with abstract methods. However, just one abstract method in a class is enough to make it an abstract class - That is, the class must be explicitly declared as an abstract class.

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

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