Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class vs. Interface

Tags:

java

c#

oop

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 Interface? 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?

like image 830
hartanto Avatar asked Feb 16 '10 06:02

hartanto


People also ask

What is the difference between a class and an interface?

Differences between a Class and an Interface:A class can be instantiated i.e, objects of a class can be created. An Interface cannot be instantiated i.e, objects cannot be created. Classes does not support multiple inheritance. Interface supports multiple inheritance.

Why use an interface instead of a class?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What is the difference between a class and an interface C#?

A Class is a full body entity with members, methods along with there definition and implementation. An Interface is just a set of definition that you must implement in your Class inheriting that Interface.

Which is better class or interface in Java?

A class can have both an abstract as well as concrete methods. Interface can have only abstract methods. Java 8 onwards, it can have default as well as static methods. Multiple Inheritance is not supported.


2 Answers

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

like image 129
Bozho Avatar answered Oct 02 '22 18:10

Bozho


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

like image 39
marklai Avatar answered Oct 02 '22 18:10

marklai