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?
-
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.Alex Hart– Alex Hart2019-08-03 11:07:50 +00:00Commented 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.VLAZ– VLAZ2019-08-03 11:30:53 +00:00Commented 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.Bergi– Bergi2019-08-03 12:32:57 +00:00Commented Aug 3, 2019 at 12:32
-
1Yes, 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.user5536315– user55363152019-08-03 13:23:14 +00:00Commented Aug 3, 2019 at 13:23
3 Answers
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.
Comments
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
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.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.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.