Abstraction and annotation in java

I am a java developer with a good understanding of object orientation concepts (or maybe I think so). And now I am studying design patterns (from the first examples of head design). I read about the abstraction of the OOPS concept in order to understand it briefly, and reading about it has become more confusing than before.

As I understand it, abstraction refers to hiding the internal details of a program, while exposing an interface to other programmers without worrying about internal details. But I do not understand

  • How abstract classes fit into this concept of abstraction, where an abstract class asks me to implement an abstract method, where the use of abstract classes in java is abstracted.
  • I feel that one way to implement the abstraction is to use a private constructor and request the class user to use the factory method to get the class object in which you can implement and hide implementation details.

Please correct me if I'm wrong somewhere.

+7
source share
2 answers

"Abstract" is the antonym of "concrete". With abstractions, you represent concepts and ideas, not a concrete way to implement these ideas. This fits into your understanding of abstraction - you hide the details and you only show the interface.

But this also corresponds to abstract classes - they are not concrete (they cannot be created for one), and they do not indicate implementations. They define abstract ideas that subclasses should take care of.

So this is basically a different point of view - one from the point of view of API clients, and the other also about subclasses. (Note that you can use abstract classes instead of interfaces in some cases to achieve the same effect, although this is not considered good practice)

+8
source
  • Abstract classes define the interface that class users will use. An abstract class is similar to an interface, except that some method can be implemented, and all abstracts will be implemented by specific classes that extend it. Thus, the advantage is that you can have multiple implementations of the same abstract class that are completely interchangeable, because the class the user is working with has an abstract type, rather than a specific implementation type.

  • Using factory methods is a general approach to abstraction, but you can also create a specific class with your constructors. What is important is the type of the variable, which must be defined as an abstract type. At the same time, access to the object variable can only be obtained using the interface defined by the abstract class.

0
source

All Articles