Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is interface highest level of abstraction?

Tags:

oop

interface

I am confused about abstraction and encapsulation.What i feel is that class is encapsulation as it encapsulates data and behaviour,while interface is abstraction.Please comment

like image 500
Rohit Raghuvansi Avatar asked Jul 29 '09 15:07

Rohit Raghuvansi


2 Answers

I think your terminology is confused.

  1. Encapsulation - puts related data and functionality in one place. We can get this through classes

  2. Polymorphism - Allows values of different data types to be handled using a uniform interface.

Polymorphism can be achieved by inheriting base classes (with virtual functions) and/or by implementing interfaces.

These techniques (and others) give us abstraction, which really applies to any of the processes we use to break a problem up into smaller components.

EDIT

Q) You ask "Can i say,abstraction is the topmost hierarchy which is accomplished through encapsulation and polymorphism?"

A) I can't answer that question, I don't know what you mean by "topmost" and "highest". There is no hierarchy here.

Functional Decomposition is a form of abstraction, it can be achieved without using Object Orientation, where should it come in the hierarchy?

The best I can do with a hierarchy is this definition (straight out of my own head, so YMMV)

  1. Abstraction is the practice of breaking a large problem into smaller components, so each smaller problem can be worked on in (relative) isolation.
  2. Polymorphism is a technique we can use to achieve abstraction. It involves identifying different types of data and behavior that can be treated in an homogeneous manner.
  3. An interface only declares types of behaviour, encapsulating the behaviour in a type. It provides no actual behaviour or data
  4. An abstract class declares types of behaviour, but may also provide behaviour and data, all encapsulated in a type.
  5. Therefore, an interface can be seen as providing a simpler or purer form of polymorphism than abstract classes.
like image 86
Binary Worrier Avatar answered Sep 24 '22 22:09

Binary Worrier


Look at the Wikipedia article on abstraction in Computer Science. In particular, you are probably interested in abstraction with respect to object-oriented programming. I've quoted part of the relevant section below:

In object-oriented programming theory, abstraction involves the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system. The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction. When abstraction proceeds into the operations defined, enabling objects of different types to be substituted, it is called polymorphism. When it proceeds in the opposite direction, inside the types or classes, structuring them to simplify a complex set of relationships, it is called delegation or inheritance.

Generally, I would say that interfaces AND classes should be examples of abstractions, dealing with the data as a conceptual "thing" rather than raw data. Encapsulation is used to make these abstractions work well with other abstractions of conceptually different "things."

like image 20
tvanfosson Avatar answered Sep 24 '22 22:09

tvanfosson