I am a java developer with good u开发者_JAVA技巧nderstanding of Object orientation concepts(or maybe, I think like that). And right now I am learning design patterns (From Head first design patterns). I have been reading about OOPS concept abstraction to understand it briefly, and reading more about it has made me more confusing than I earlier was.
As I understand, abstraction refers to hiding the internal details of the program while exposing the interface to other programmers without worries of internal details. But, I don't understand
- How abstract classes fit into this concept of abstraction, where abstract class asks me to implement the abstracted method, where is abstraction in using abstract classes in java.
- I feel that, one way in which abstraction can be implemented is through private constructor and asking user of the class to use factory method to get the object of the class where you can implement and hide implementation details.
Please correct me, If I am wrong anywhere.
"Abstract" is an antonym of "concrete". With abstractions you represent notions and ideas, rather than the concrete way these ideas are implemented. This fits into your understanding of abstraction - you are hiding the details and you only show the interface.
But this also fits with abstract classes - they are not concrete (they can't be instantiated, for one), and they don't specify implementations. They specify abstract ideas that subclasses have to take care of.
So it is basically a different point of view - one is from the point of view of clients of the API, and the other is also about subclasses. (Note that you can use abstract classes instead of interfaces in some cases to achieve the same effect, although it's not considered good practice)
The abstract classes defines an interface which the users of the class will use. An abstract class is similar to an interface except that some method can be implemented and all the abstracts ones will be implemented by the concrete classes that extends it. In summary the advantage is that you can have multiple implementations of the same abstract class that are completely interchangeable because the class that user operate with, is of the abstract type rather than of a specific implementation type.
Using factory methods is an common approach for abstraction but you can also instantiate concrete class with their constructors. The important thing is the type of the variable that must be defined as the abstract type. Doing so the object variable can be accessed only with the interface defined by the abstract class.
精彩评论