Class vs. Interface

后端 未结 10 1440
孤城傲影
孤城傲影 2020-12-10 03:18

I have a quite basic question:

When should we decide to use Interface or Class for a specific class?

For example: says we have 2 classes, Customer and Doctor

相关标签:
10条回答
  • 2020-12-10 03:26

    In object modeling, one shouldn't use inheritance for people and their roles. One should instead model them with two associated objects. That is, using composition instead of inheritance.

    So in a way, of three alternatives, your discussing the two wrong ones: inheritance and interfaces for modeling parties and roles.

    A class is a template for a set of objects. Those objects have behavior, (and typically) state and characteristics. In regards to behavior, each object has an (implicit) interface already: the set of methods that can be externally called on it.

    The question then becomes, why should you create a named interface, a subset of the interface already provided by an object?

    1. One wants to represent a subset of behavior so different classes of objects can be treated polymorphically.
    2. One wants to constrain the possible behavior of an object exposed to another, to a subset of its (implicit) interface because the object is acting in a different context.
    0 讨论(0)
  • 2020-12-10 03:29

    Think of the interface as a contract. The class may commit to the contract (implement the interface)

    Suppose you have class Person with subclasses Doctor and Patient. Then you'd have interface Treatable with the method getSymptoms() implemented by Patient and interface Treating with method cure(Treatable) implemented by Doctor. Most likely cure(Treatable) would call getSymptoms() at some point ...

    0 讨论(0)
  • 2020-12-10 03:30

    In C#, multiple inheritance can be achived through Interface only. Based on u r business requirement if there is a need that your class needs multiple inheritance going fwd, then use Interface else use class.

    Also all members of Interfaces should be given defination in class i.e. interface members are must-implemented members.

    0 讨论(0)
  • 2020-12-10 03:39

    class suggests that object that inherits base class is some kind of this class

    if you use interface it only shows that your class have some common behaviour that interface describes

    0 讨论(0)
  • 2020-12-10 03:41

    Parent Class is the one which will have bare minimum properties common to all of its sub classes.

    But Interface is a contract which tells its implantations to provide if it is not an abstract class.

    And the One important difference between a class and interface is that

    class inheritance will give relation between two common subclasses.

    Where as Interface implementation gives a relation between two uncommon classes.

    0 讨论(0)
  • 2020-12-10 03:42

    First thing to remember, Classes can be instantiated, Interfaces cannot.

    Secondly, a Class can only extend ONE Class. Interfaces are not limited by this and so we can have multiple inheritance like so

    public class foo extends Person implements Man, Mammal
    

    foo is a Person. It is also a Man and a Mammal;

    The only issue is Interfaces cannot have variables or method implementations where as a class(or abstract class for that matter) can.

    Generally I would say stick with interfaces and avoid abstract classes if you can.

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