What is the difference between an interface and abstract class?

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

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

30条回答
  •  不要未来只要你来
    2020-11-21 12:20

    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/

提交回复
热议问题