开发者

Class vs. Interface

开发者 https://www.devze.com 2022-12-20 02:08 出处:网络
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.

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.

In Inheritance (class): we could set these 2 classes to inherit from parent class Person.

Couldn't we do the same with Int开发者_Python百科erface? Says we have InterfacePerson and have both Customer and Doctor implement the interface?

Thus, this lead to: when do we decide to use one over the other and vice versa?


  • Read the wikipedia article

  • Read a book, then read the chapters about OOP again

  • In your example, Person should be a class, because it contains implementation details that are common to both a Doctor and a Customer.

  • interfaces don't have (and don't need) implementation details - they only denote what objects which implement them are doing. Not how. Why is this useful? Because when you are using the object you don't care how it's going to do its job.

Let's take a look at a simple example - there is an interfaces Comparable (in Java at least). It denotes that its implementors can be compared with each other. So you can have two classes:

class Doctor implements Comparable {..}

class Customer implements Comparable {..}

Now you can have a common method which takes any set of objects which implement Comparable and call comparable1.compareTo(comparable2), because you know they can perform comparison - it's denoted by their interface.


  • Interface - describe the behavior
  • Class - do the behavior

A class extending another class inherits the behavior. On the other hand, implementing an interface just says it need to behave that way but the class still has to know the how-to.

Besides single inheritance limitations, code using interfaces are easier to refactor and test, e.g. provide a mock implementation for a database access object in unit tests.

So, the real answer is, it depends on your design.

It may be that you use interfaces to describe behavior, and abstract parent classes to implement the behavior that can be inherited by subclasses. Or maybe the subclasses are so different that each implements the interface in their own way.


Interfaces are used to enforce certain Methods/Properties. In a nutshell- an interface is a set of rules.

A class can be used to inherit/override base functionality.

Have a look at

  • Difference between class and interface in C#?
  • C#: Interface vs. Class
  • Abstract Class versus Interface
  • interface (C# Reference)


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.


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.


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.


Simply put, you use classes when there is code/implementation involved and interfaces when it is just interface descriptions. When referring to objects in your code, prefer to refer to the interfaces as it makes it easier to replace the actual implementation (or add more and different implementations).

And yes, both Docor and Customer/Patient are likely to implement or extend Person.


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 ...


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号