What is the difference between an interface and abstract class?

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

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

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

    An Interface contains only the definition / signature of functionality, and if we have some common functionality as well as common signatures, then we need to use an abstract class. By using an abstract class, we can provide behavior as well as functionality both in the same time. Another developer inheriting abstract class can use this functionality easily, as they would only need to fill in the blanks.

    Taken from:

    http://www.dotnetbull.com/2011/11/difference-between-abstract-class-and.html

    http://www.dotnetbull.com/2011/11/what-is-abstract-class-in-c-net.html http://www.dotnetbull.com/2011/11/what-is-interface-in-c-net.html

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

    Not really the answer to the original question, but once you have the answer to the difference between them, you will enter the when-to-use-each dilemma: When to use interfaces or abstract classes? When to use both?

    I've limited knowledge of OOP, but seeing interfaces as an equivalent of an adjective in grammar has worked for me until now (correct me if this method is bogus!). For example, interface names are like attributes or capabilities you can give to a class, and a class can have many of them: ISerializable, ICountable, IList, ICacheable, IHappy, ...

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

    You can find clear difference between interface and abstract class.

    Interface

    • Interface only contains abstract methods.
    • Force users to implement all methods when implements the interface.
    • Contains only final and static variables.
    • Declare using interface keyword.
    • All methods of an interface must be defined as public.
    • An interface can extend or a class can implement multiple other interfaces.

    Abstract class

    • Abstract class contains abstract and non-abstract methods.

    • Does not force users to implement all methods when inherited the abstract class.

    • Contains all kinds of variables including primitive and non-primitive

    • Declare using abstract keyword.

    • Methods and members of an abstract class can be defined with any visibility.

    • A child class can only extend a single class (abstract or concrete).

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

    Some important differences:

    In the form of a table:

    Difference

    As stated by Joe from javapapers:

    1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.

    2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

    3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..

    4.Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

    5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

    6.A Java class can implement multiple interfaces but it can extend only one abstract class.

    7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.

    8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

    0 讨论(0)
  • Key Points:

    • Abstract class can have property, Data fields ,Methods (complete / incomplete) both.
    • If method or Properties define in abstract keyword that must override in derived class.(its work as a tightly coupled functionality)
    • If define abstract keyword for method or properties in abstract class you can not define body of method and get/set value for properties and that must override in derived class.
    • Abstract class does not support multiple inheritance.
    • Abstract class contains Constructors.
    • An abstract class can contain access modifiers for the subs, functions, properties.
    • Only Complete Member of abstract class can be Static.
    • An interface can inherit from another interface only and cannot inherit from an abstract class, where as an abstract class can inherit from another abstract class or another interface.

    Advantage:

    • It is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
    • If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
    • If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
    • Its allow fast execution than interface.(interface Requires more time to find the actual method in the corresponding classes.)
    • It can use for tight and loosely coupling.

    find details here... http://pradeepatkari.wordpress.com/2014/11/20/interface-and-abstract-class-in-c-oops/

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

    In practicality terms(JAVA), the major difference between abstract class and interface is Abstract class can hold state. Other than holding state we can achieve rest operations with Interface also.

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