nus-cs2030 / 2122-s2

CS2030 repository and wiki for AY 2021/2022 Sem 2
MIT License
0 stars 0 forks source link

nani is abstraction??? #57

Open GraceYongXM opened 2 years ago

GraceYongXM commented 2 years ago

Description

could anyone kindly explain what is data abstraction and functional abstraction? preferably with examples too!! thanks peeps

Topic:

abstraction

Screenshots

Screenshots (if any) `

Darien2307 commented 2 years ago

Hi, hmm let's use the classic Circle object as an example.

Circle has the attribute, radius. Knowing this attribute, we can abstract the data. The client doesn't need to know how the data is being stored, he/she only needs to know that "Ah, circle actually has a radius attribute!"

Circle may have methods like computeArea() i.e. πr². Functional abstraction comes in where the client doesn't need to know how the area is being calculated, they only need to know computeArea() will give the correct area of a circle with a certain radius. Here, he/she will be like "Ah, I want a circle with radius 1. Oh cool, after inputting in computeArea(), I now know circle of radius 1 has an area of 3.141....

Hope my understanding is correct as well.

shotnothing commented 2 years ago

To add on, from my understanding, the philosophy of data abstraction is to separate the client (i.e. someone who wants to find radius, a number) from the implementation (e.g. double, int, Integer, etc.). For example, you want to make a shopping list. It has stuff that you expect to be able to do like add/remove items, lookup items, check the length etc. How this may be implemented in Java is by linkedlists, arraylists, etc., but the client should not need to know about those things to be able use a shopping list, nor should they care. All the client needs to know are what they can do to the shopping list, not how the data is stored.

summerthia02 commented 2 years ago

Every time you create or declare a function / method, you are creating an abstraction (functional abstraction) by assigning a name to that method for e.g. distanceTo(point). Clients are able to use this abstraction by calling your function, they do not need to know how your code is implemented / designed. So long as your code works and that I can compute a distance from point p to q using p.distanceTo(q) for instance, function abstraction is done well.

Data abstraction is too surrounding the idea of how clients can you your data structure / data without knowing how you implemented it / how the data is stored. For instance, in lecture 2, we learnt about the data type Point. Point is obviously now a primitive data type in Java, however, we can create a point using multiple methods like List coord; double[] coord; and now (x,y) is considered a point that can be used by clients. so, the implementer has the responsibility to create this data structure. Another example would be ImList. we can use the ImList.sort(), ImList.size(), without actually knowing how it was implemented (yay)

Lu-Yi-Fan commented 2 years ago

Instead of writing a very customised chunk of code that only does one thing. The idea of abstraction is to write methods and customisable functions to make your chunk of code more customisable so that you can reuse the methods/functions instead of re writing repeated codes.

JiaEn182445u commented 2 years ago

Data Abstraction <- Information hiding. The client don't need to know how/where the implementer store the data.

Functional Abstraction <- Allow the client to call the function implemented by the implementer without knowing how the function algorithm/ calculation works.