Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstraction and abstract in java

I am a java developer with good understanding 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

  1. 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.
  2. 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.

like image 684
Vishwanath Avatar asked Mar 26 '11 15:03

Vishwanath


People also ask

What is abstractions in Java?

Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message.

Why abstraction is used in Java?

Abstract Class in Java does the process of hiding the intricate code implementation details from the user and just provides the user with the necessary information. This phenomenon is called Data Abstraction in Object-Oriented Programming (Java).

What is abstraction and its types in Java?

In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces. Abstract classes and Abstract methods : An abstract class is a class that is declared with an abstract keyword. An abstract method is a method that is declared without implementation.

What are the four types of abstraction?

There are four types of abstracts: informative, descriptive, critical, and highlight abstracts. However, students most often use informative abstracts.


1 Answers

"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)

like image 87
Bozho Avatar answered Oct 12 '22 13:10

Bozho