3

If abstraction is defined as hiding implementation details, then would that mean that calling the toString method is an example of abstraction?

4 Answers4

2

In java, the root class Object has a default toString method, every sub class will inheritate this method(although usually it is overrided), it is an example of abstraction.

xingbin
  • 27,410
  • 9
  • 53
  • 103
2

Yes.

Since toString is an instance method defined by Object class, its invocation is virtual. Hence, it hides the details of the behavior from the caller, making it an example of abstraction.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Abstraction means adding only those components in your design/code that are required by your application.

Hiding implementation details is encapsulation.


Implementing toString() is more an example of Encapsulation. Reasoning: The object decides what information about self, does it want to give to the output stream.


See this for detailed explanation.

displayName
  • 13,888
  • 8
  • 60
  • 75
1

Abstraction means, define relevant part of real world. You probably mix the typical OOP language keyword abstract, that is rather a declaration of interface. As wikipedia says:

Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations that all objects implementing the protocol must support.

So it is an abstraction in terms of OOP principles, but every model of the real world you create, is an abstraction, so defining a class.

Hiding implementation details is rather part of the encapsulation.

Correct me, if I am wrong.

bmolnar
  • 171
  • 8