-1
$\begingroup$

I think that everyone will agree that the following class achieves data abstraction (hiding of its internal representation):

class A {
    private List x = new ArrayList();

    public List getX() {
        return this.x;
    }

    public void setX(List x) {
        this.x = x;
    }
}

But could we say the same about the following one, which uses a class (instead of an interface like in the previous class) as a field type, parameter type, and return type?

class B {
    private ArrayList x = new ArrayList();

    public ArrayList getX() {
        return this.x;
    }

    public void setX(ArrayList x) {
        this.x = x;
    }
}
$\endgroup$
7
  • $\begingroup$ Cross-posted: cstheory.stackexchange.com/q/49207/5038, cs.stackexchange.com/q/142051/755. Please do not post the same question on multiple sites. $\endgroup$
    – D.W.
    Jul 6, 2021 at 3:55
  • $\begingroup$ Personally i wouldn't even consider the first one as data abstraction $\endgroup$
    – nir shahar
    Jul 6, 2021 at 4:39
  • $\begingroup$ You have tagged your question with both abstract-data-types and object-oriented. That is somewhat confusing because ADT-based data abstraction and object-oriented data abstraction are very different, and in some sense even opposites. You should be clear about which one of the two you mean. See for example On Understanding Data Abstraction, Revisited by William R. Cook for a simple explanation. $\endgroup$ Jul 6, 2021 at 6:44
  • $\begingroup$ @JörgWMittag I am currently reading Luca Cardelli’s paper On understanding types, data abstraction, and polymorphism, which prompted William Cook to write his paper on the subtle distinction between abstract data types and objects. Since I have not started reading William Cook’s paper, I did not want to exclude any possibilities, especially because I feel that the answer is precisely about this distinction. Could you write an answer? $\endgroup$
    – Maggyero
    Jul 6, 2021 at 7:22
  • $\begingroup$ To be honest, the example might be too simplistic. Especially since OO is all about behavior, and OO-abstraction is behavioral abstraction, but there is no behavior in your example. $\endgroup$ Jul 6, 2021 at 8:22

1 Answer 1

2
$\begingroup$

Sounds subjective to me. Data abstraction is not a black-and-white binary property; it is a matter of shades of grey. Any abstraction hides some information and reveals others.

Another way to put it is that these two classes expose a different amount of functionality and make a different set of promises. So, what do you want the client to be aware of, and what do you want them to not have to know about? What future changes do you want to allow yourself to make to the class (without having to change clients), and what future changes are not important to be able to make? The answers to those questions will determine which of these two implementations you choose. Each could be a reasonable choice that is consistent with the principle of data abstraction in different circumstances.

Also, remember that abstraction is a means to an end, not a goal in and of itself.

See, e.g., https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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