[ACCEPTED]-Class vs. Interface-oop

Accepted answer
Score: 15
  • Read the wikipedia article

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

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

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

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

class Doctor implements Comparable {..}

class Customer implements Comparable {..}

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

Score: 13
  • Interface - describe the behavior
  • Class - do the behavior

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

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

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

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

Score: 10

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

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

Have a look at

Score: 3

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

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

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

The 3 question then becomes, why should you create 2 a named interface, a subset of the interface 1 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.
Score: 2

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

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

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

class inheritance will 3 give relation between two common subclasses.

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

Score: 2

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

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

public class foo extends Person implements Man, Mammal

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

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

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

Score: 1

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

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

Score: 1

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

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

Score: 0

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

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

Score: 0

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

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

More Related questions