3

I have some years of oop programming experience,I though I know what abstraction is (using abstract class and interface), but I am confused with the definition that appear in a book which says:

"The process of filtering the available information and arriving at a subset that is essential for your application is called abstraction.

and

"Abstraction can be achieved using Abstract Class and Abstract Method."

According to the first part, let's say we want to create a banking application and we are asked to collect all the information about your customer. customer info can contain: name, gender, age, favorite food, favorite movie etc... so we select only the useful information for the banking application from that pool and pick up name, gender and age.

But why it has to be achieved using Abstract Class and Abstract Method? we can just have a normal class as:

public class Customer
{
   string name;
   string gender;
   int age;
}

so we don't even use abstract class, so how Abstract Classes associated with filtering information?

  • 2
    "I know what abstraction is (using abstract class and interface)" Abstract classes and interfaces are a mean to achieve abstraction, but they do not define what abstractions are. – Vincent Savard Jan 31 at 12:36
  • Try to come up with your own definition of what an abstraction is. Once you figure that out, you’ll probably understand the definition in the book. – Rik D Jan 31 at 12:49
  • 1
    Here's an abstraction that doesn't involve an abstract class or an interface. System.Format("{0}, {1}!", "Hello", "World"); - returns"Hello, World!". The syntax of the format string is an abstract language (an abstraction) that defines how the parameters are to be transformed & composed. The details that are omited ("filtered out") are everything related to the actual algorithm that performs the task. Therefore, client code doesn't depend on those details, which in turn makes it possible to change the internals of Format without breaking clients - it's the Dependency Inversion Principle – Filip Milovanović Jan 31 at 13:12
  • Go-to CS 61A course, UoC Berkeley. They have separate topic on abstraction – overexchange Jan 31 at 14:06
  • 1
    "why it has to be achieved using Abstract Class and Abstract Method" - short answer: it doesn't have to, and it is actually not what your cite says ("can be achieved", and not "has to be achieved"). So I am not sure if this just a problem of not reading properly. Can you please edit the question and clarify? – Doc Brown Jan 31 at 17:07
6

Language features like interfaces and abstract classes and methods are "just" tools. Using them is not the actual act of abstraction.

An abstraction as the book is correct to point out is the act of hiding all irrelevant information to the caller.

So in your banking example the question becomes what is relevant for the user code regarding the Customer. More often than not, the data is actually not that important, the important part is what it can do. For example it can present itself on the web GUI. This is called behavior in object-orientation terms. In these cases, using an "interface" (i.e. abstract classes and methods) might be a good idea.

In some rare cases the data itself might be relevant to the caller, in these cases the class you presented is the abstraction. No interfaces need be involved.

| improve this answer | |
6

There are two different ideas here - there's the Concept of Abstraction, but there's also abstract as a Programming language feature which can be found in popular languages such as Java and C#, usually enabled by a language keyword.

Abstraction as a concept can be summarised as "exclusion of irrelevant detail", which is something that can be applied in many different contexts, including many which have absolutely nothing to do with classes or language features which involve an abstract keyword.

In the given example, class Customer is an abstraction of something which exists in a domain (e.g. a Customer for a real-world retailer) - it contains a limited amount of data, and therefore excludes irrelevant details which may happen to be true of the domain or the real world, but are not useful or relevant to the code which needs to know about the Customer's name, gender and age.

For example, the class does not contain data about the colour of their hair or how many toes they have, so the class is a representation which abstracts those things away, and only represents useful, relevant data.

| improve this answer | |
2

The process of filtering the available information and arriving at a subset that is essential for your application is called abstraction.

That is an unfortunately obtuse definition.

Let's examine a real-world example of abstraction. You own a car. Due to the valiant efforts of many people, you don't have to build an engine each time you want to drive your car, nor do you particularly care about how that engine works. You just drive your car. The finer details about how the engine works are considered implementation details.

So it goes with software. A Hash Table is an abstraction over a list of items that allows you to look up items rapidly from that list using a key. You don't have to know how the hash table works, although knowing the broad brush strokes does allow you to use a Hash Table more effectively, just as understanding how a car works can make you a better driver.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.