Difference between abstract class and interface

abstract class extends

Abstract method:abstract void fun()

An abstract class is abstracta class decorated with. Because there are no concretely implemented methods in an abstract class, it cannot be instantiated; an abstract class does not necessarily contain abstract methods;

abstract class fun{
    abstract void fun();
}

important point:

  • The abstract method must be publiceither or protected(because privateit cannot be implemented if it is a subclass);
  • If a class inherits from an abstract class, the subclass must implement the abstract method of the superclass. If the subclass does not implement the abstract method of the superclass, the subclass must also be defined as an abstract class;
  • Abstract classes cannot create objects.

interface implements

An interface is an abstraction of behavior, and an interface can contain methods and variables. The variable must be public static finaldecorated with; the method must be public abstractdecorated with.

interface fun(){}

Difference between abstract class and interface

  • Variable: any type (abstract class) / public static final(interface)
  • Method: any type - abstract method, method (abstract class)/ public abstract(interface)
  • An abstract class is an abstraction of things, an interface is an abstraction of a behavior

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215964&siteId=291194637