-2

I was recently told that I should use abstraction more in my code. I am fairly unfamiliar with the matter, and in research I have found a lot about abstract classes but I am referring more to the "information hiding" portion, not the function in the code itself.

For example, if I have one class which performs a unique purpose, why would I want to create an abstract version for that class to implement? I am having trouble figuring out when to use abstraction and for what purpose, how does having essentially the same class without method bodies change how it is used? Is it mostly for when other people work with the code, and if that is true, should it still be done in more private projects just as good practice?

Ian Ryan
  • 3
  • 4
  • to hide the complexity. for example,we just know when push number 1 button on remote, it goes to channel 1 and we do not know how it goes because the maker of the remote hide the process from us. – Kick Buttowski Dec 01 '17 at 01:42
  • You might want to have your actual class extend an abstract class (or implement an interface; both are pretty similar) if you anticipate that other similar problems might come up later. In that case, you could move some common functionality which you expect all problems to have into the abstract class and then all children classes can reuse it. – Tim Biegeleisen Dec 01 '17 at 01:43
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking!"* –  Dec 01 '17 at 01:46
  • Please read [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) before attempting to ask more questions. –  Dec 01 '17 at 01:46
  • @JarrodRoberson I apologize, I had looked at dozens of threads (including the marked ones) and still could not comprehend its usage; to me this was adequate to ask for further explanation but I now understand that this was in poor taste for the community and I will do more work before asking a question in the future. – Ian Ryan Dec 01 '17 at 13:34

1 Answers1

0

Abstraction is a more general notion in programming than declaring classes as abstract.

Abstraction is related to separation of concerns, and basically involves encapsulating details of implementation away from higher level, or more abstract code.

For example, I may have a user object, persisted to a database, and want to check to see if the user has some flag set to true in order see a button on a view.

I could, in my view code, get a db connection, run a sql query, deserialize the results into a user object, and check the flag, but I haven't separated my concerns. My view code knows lots about how I persist data, how to take a resultset and turn it into a user, how to get a db connection, the db structure, etc...

Instead I should abstract away those details, so the view layer can just get the user from something and check the flag. The something can know the details of the database, and may even delegate some of those details to lower level components.

Taylor
  • 3,942
  • 2
  • 20
  • 33
  • So am I understanding correctly in that abstraction isn't even limited to abstract/interface classes (Java) but could be done just by having dedicated classes for each separate concern? Like in your example, having the view layer call a method in some user handler which in turn calls a method in an SQL class, returning a resultset which the user handler deserializes and returns to the view code (maybe not perfect but general idea). At that end, are abstract/interface classes necessary outside of polymorphism or is said example sufficient for simple abstraction? Please pardon my confusion. – Ian Ryan Dec 01 '17 at 13:25
  • Polymorphism and composition are two techniques to apply abstraction. The wikipedia article here: https://en.wikipedia.org/wiki/Abstraction_(software_engineering) does a decent job of explaining. – Taylor Dec 01 '17 at 18:24