3

I was studying the object oriented concepts and there the abstraction concept is basically described as hiding the implementation from user. So if there is a member function in a class and we call that function for some task, abstraction says that user should not be concerned about how things are getting done but should only be aware of what is getting done. But even in a non object oriented programming style, if we write a function, the whole task is accomplished by simply calling a function. Doesn't it follow the abstraction logic too? Or, is there any difference between the abstraction in OOP and functional programming?

4
  • Abstraction is an extremely broad topic but for oop I would learn about polymorphism. The same concepts work in FP in a way, where your "interface" is say, a typeclass or a function signature. Commented Aug 3, 2019 at 11:07
  • Yeah, abstraction is to keep the user from the details. You can achieve that in functional programming, too. In fact, a lot of times you may not know nor care how a particular function was derived, e.g., might have been composed from different other functions. So, abstraction holds up in both cases - it's about keeping details away from calling code. The difference is when abstraction is used in conjunction with other techniques - as @AlexHart says, it comes into play a lot with polymorphism. Commented Aug 3, 2019 at 11:30
  • "hiding the implementation from the user" is what I would call encapsulation or information hiding. And yes, functions/procedures do that as well, you don't necessarily need objects for it - what makes the concept important for OOP is that an object is the unit of encapsulation. Commented Aug 3, 2019 at 12:32
  • 1
    Yes, a function abstracts from an expression quite naturally and it also generalizes it. But it does all this without state and without further effects than mapping the input to the output. There is no binding with data but merely an expectation as to what type that data has. If you are looking for distinctions between both paradigms you should look into the opposite concept: reification. FP is basically programming with values. Functions are values. Even effects are represented by and encapsulated in values. I think there is no counterpart in OOP - I am no expert though. Commented Aug 3, 2019 at 13:23

3 Answers 3

1

In Object Oriented Programming, we generally think about Abstractions in terms of inheritance and polymorphism.

Let's consider the Writer interface

interface Writer {
    void write(byte[] bytes)
}

This interface allows a user to write to... something. What, we're not particularly worried about. We could have multiple versions of this:

class FileWriter implements Writer

class StringWriter implements Writer

class LogWriter implements Writer

class MySuperCustomWriter implements Writer

Where we write isn't important, be it a File, String, a socket, or wherever. All we want to do is write to something. This lets us write code like this:

public class MyBusinessLogic {

    private final Writer writer;

    public MyBusinessLogic(Writer writer) {
        this.writer = writer;
    }

    void someBusinessLogic() {
        // .. 
        writer.write(someStuff);
    }
}

What we have here is some business logic that wants to do some writing. By using an interface, our business logic is no longer dependent on any specific method of writing. It just gets some object that is capable of doing some writing. We can pass it any of our example writers and be sure that it'll work right, because we're interested here in the behavior of writing, and not the implementation.

By doing this, the business logic is not dependent on the file system, network, or anything else.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not the fact that you call a function that is providing the abstraction but the manor in which it's called. For example, you might have a function which allows you to write a line of text:

void writeLine(string fileName, string value)

But that is not abstracted from the fact that you're writing to a file. The abstracted version would not require the caller to provide the fileName parameter because that is specific to that particular implementation of the function. Instead you would have:

void writeLine(string value)

And the fileName is provided using another mechanism e.g. a constructor argument of a class if you're using OOP and calling a writeLine method or, in the functional case, you might curry the original function to create the abstracted version.

3 Comments

"But that is not abstracted from the fact that you're writing to a file." yet the user is abstracted from the actual implementation. E.g., whether the data is chunked into 512 byte chunks to write or something else. That is what abstraction is about - implementation details. Whether or not you write to a file can also be "hidden" - e.g., by having an interface Writer and then different implementation FileWriter and ConsoleWriter, yet that is not what the core of abstraction is about. The user doesn't know nor care the precise manner the line is written and that's what's important.
The act of calling a function is not providing abstraction if the function requires you to pass in the string to write, the file name, the chunk size, etc. The caller need to know all these details which are important to the implementation which means it is not at all abstracted from the implementation. The important part is to not require the caller to need to provide anything more than the information they already possess. To not require the caller to change in order to be able to call the function.
That is correct and providing the filename is perfectly viable. For example, the user when interacting with the problem hits "Save" and is prompted for the filename - in that case, the code that calls the method to write the information will know what the filename is. Contrast that with, say, a logger - the calling code will generally not know not care where that output goes - it will just call log(someData) and that will then be output to a file, the console, a network location, maybe more than one at once. So, a method that takes a file name does not inherently lack abstraction.
-1

Quick Encapsulation Example

type
    public class DateTimeClass
       private 
          Era: integer;
          Culture: integer;
          Year: integer;
          Month: integer;
          Day: integer;

      protected
           Integer function getYear ( );

           // other functions

           procedure setYear ( );

          // other functions
      public
          procedure AssignOccidentalDate
               (NewYear: integer; NewMonth: integer;
                    NewDay : integer);
   end;

   ...

   var Date: DateTimeClass;

  Date.AssignOccidentalDate (2019, 07, 27);
  ...

You can only access the "public" declarations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.