Understanding abstraction

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?

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật