What is the difference between an interface and abstract class?

前端 未结 30 1777
情歌与酒
情歌与酒 2020-11-21 11:51

What exactly is the difference between an interface and abstract class?

相关标签:
30条回答
  • 2020-11-21 12:21

    Let's work on this question again:

    The first thing to let you know is that 1/1 and 1*1 results in the same, but it does not mean that multiplication and division are same. Obviously, they hold some good relationship, but mind you both are different.

    I will point out main differences, and the rest have already been explained:

    Abstract classes are useful for modeling a class hierarchy. At first glance of any requirement, we are partially clear on what exactly is to be built, but we know what to build. And so your abstract classes are your base classes.

    Interfaces are useful for letting other hierarchy or classes to know that what I am capable of doing. And when you say I am capable of something, you must have that capacity. Interfaces will mark it as compulsory for a class to implement the same functionalities.

    0 讨论(0)
  • 2020-11-21 12:21

    The shortest way to sum it up is that an interface is:

    1. Fully abstract, apart from default and static methods; while it has definitions (method signatures + implementations) for default and static methods, it only has declarations (method signatures) for other methods.
    2. Subject to laxer rules than classes (a class can implement multiple interfaces, and an interface can inherit from multiple interfaces). All variables are implicitly constant, whether specified as public static final or not. All members are implicitly public, whether specified as such or not.
    3. Generally used as a guarantee that the implementing class will have the specified features and/or be compatible with any other class which implements the same interface.

    Meanwhile, an abstract class is:

    1. Anywhere from fully abstract to fully implemented, with a tendency to have one or more abstract methods. Can contain both declarations and definitions, with declarations marked as abstract.
    2. A full-fledged class, and subject to the rules that govern other classes (can only inherit from one class), on the condition that it cannot be instantiated (because there's no guarantee that it's fully implemented). Can have non-constant member variables. Can implement member access control, restricting members as protected, private, or private package (unspecified).
    3. Generally used either to provide as much of the implementation as can be shared by multiple subclasses, or to provide as much of the implementation as the programmer is able to supply.

    Or, if we want to boil it all down to a single sentence: An interface is what the implementing class has, but an abstract class is what the subclass is.

    0 讨论(0)
  • 2020-11-21 12:22

    In short the differences are the following:

    Syntactical Differences Between Interface and Abstract Class:

    1. Methods and members of an abstract class can have any visibility. All methods of an interface must be public. //Does not hold true from Java 9 anymore
    2. A concrete child class of an Abstract Class must define all the abstract methods. An Abstract child class can have abstract methods. An interface extending another interface need not provide default implementation for methods inherited from the parent interface.
    3. A child class can only extend a single class. An interface can extend multiple interfaces. A class can implement multiple interfaces.
    4. A child class can define abstract methods with the same or less restrictive visibility, whereas class implementing an interface must define all interface methods as public.
    5. Abstract Classes can have constructors but not interfaces.
    6. Interfaces from Java 9 have private static methods.

    In Interfaces now:

    public static - supported
    public abstract - supported
    public default - supported
    private static - supported
    private abstract - compile error
    private default - compile error
    private - supported

    0 讨论(0)
  • 2020-11-21 12:23

    An explanation can be found here: http://www.developer.com/lang/php/article.php/3604111/PHP-5-OOP-Interfaces-Abstract-Classes-and-the-Adapter-Pattern.htm

    An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

    An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class sub-classing from it, it is said to implement that interface.

    Anyway I find this explanation of interfaces somewhat confusing. A more common definition is: An interface defines a contract that implementing classes must fulfill. An interface definition consists of signatures of public members, without any implementing code.

    0 讨论(0)
  • 2020-11-21 12:24

    To give a simple but clear answer, it helps to set the context : you use both when you do not want to provide full implementations.

    The main difference then is an interface has no implementation at all (only methods without a body) while abstract classes can have members and methods with a body as well, i.e. can be partially implemented.

    0 讨论(0)
  • 2020-11-21 12:24

    In an interface all methods must be only definitions, not single one should be implemented.

    But in an abstract class there must an abstract method with only definition, but other methods can be also in the abstract class with implementation...

    0 讨论(0)
提交回复
热议问题