I am trying to understand object oriented code better and I decided to start at abstraction. If I am not incorrect, abstraction means that you hide information that isn't relevant to what task you want to perform, i.e. If I turn on the TV I would not need to know the internal that is going on.

Or If I have a class that handles Orders, and I would like to get the Total cost of a couple of Products, a getTotal() method would be the only thing that the User needs to know about to get the total sum of the products, and not how it's actually calculated.

So per se, by using a class I am abstracting data?

1 Answer 1


Hiding data is more Encapsulation, really. Abstraction is about hiding implementation details.

For example, let's say we want to add two numbers together. Unless you've done a fair bit of studying of electronic engineering, you probably have no idea just how complex such a simple problem is.

First, the compiler turns your instruction x = y + z into machine code that loads two values into two registers in the CPU, then executes an ADD instruction, retrieves the result from the register, and stores it back into memory. And that ADD instruction requires some complicated logic gates to function properly, and those logic gates are built out of transistors and semiconductors, which have to be engineered to very exacting specifications based on the laws of quantum mechanics...

...but you don't really care about any of that. All you care about is being able to write the code and get the right answer. The programming language is an abstraction that covers up the details of how the machine code really works, and the machine code is an abstraction that covers up the details of how the underlying electronics work, and so on. As long as it works, you don't really need to worry about how, and that makes your job as a programmer a lot simpler!

On a somewhat higher level, let's say you want a way to save data to a file, and load it from a file. Well, there are filesystem APIs in your OS for that. But what if you also want your program to be able to download a file from the Internet and process it? You could always download it and save it to a file, and then use your code to load it from a file, but that's kind of wasteful and overly complicated, especially if the user doesn't actually need your program to leave that file on their hard drive.

So instead, you can use an abstraction: a stream. The idea of a stream is "I have this sequence of data that can be read or written to in order." Note how it doesn't say anything about the underlying representation of the data. So if you rework your code to deal with streams instead of files, it suddenly becomes much more versatile. Now you can read from a file by giving the code a FileStream object, or work with a download by giving the exact same code a HTTPStream object, or even work with data held entirely in local memory with a MemoryStream object. The code that's processing the stream doesn't need to know anything about files anymore; it just needs to know about the data that the stream contains. By abstracting away the details ("the data is saved to a file") your job as a programmer becomes easier, and your code becomes more flexible.

  • Yes, a "interface" that will return the getTotal() would be an abstraction, the user would ask for, I would want the Total sum of this products, not how he should calculate it (using a foreach loop and a +), i.e. Abstraction in programming seems to me to hide the implementation, give the User a simple interface to retrieve what he wants, that's per if, I have a Car class and I would like to know the Color I wouldn't want back that the car is a Volvo with a red color, just red color. Your example was a bit more advanced perhaps, but I would just like to know that we are on the same page. Nov 16, 2013 at 8:51

Your Answer

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

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