Monday, September 2, 2013

Abstraction in OOPS | How Abstraction can be achieved in JAVA


Abstraction:


We all know that abstraction is one of the characteristics of Object Oriented Programming System (OOPS). If you ask someone "what is abstraction?" the most common possible answer that you hear is .. "Abstraction is hiding some important features or data ..bla ..bla..bla "

The question is ..what is hiding ..? where you are hiding..? which part you are hiding ..?

Is hiding something is the correct definition for abstraction ?

then what it (abstraction) really means?

Here it goes:


In simple words i can say.. "Abstraction implies providing something.. but not everything"

i will consider one real time example to illustrate it in much better way.




Suppose there is an aeroplane flying in the sky. We know that it is flying, but we don't know how exactly it is flying and what are the underlying mechanisms, working and all. 

this is what we are looking for.. "abstraction"

How to achieve Abstraction in Java:


By using abstract classes or interfaces , we can achieve abstraction in Java.
As we all know that abstract classes, interfaces contains abstract methods (ofcourse abstract classes may or may not contain) i.e , only method declarations.

By looking at the method declaration we can estimate what exactly the method is doing, what it is going to return. But we don't know how exactly the abstract method will be implemented.

We came to know about the implementation, once we provided the method implementation in the classes which implement the corresponding abstract class or interface.

Example for the above illustration:


Let us consider an interface ( you can take abstract class also) Addicted.




In the above interface, getAddictedListOfPersons() is the abstract method (by default all the methods in an interface are public abstract ) which returns list of all the addicted persons. By looking at the method declaration we can infer this.
(As per the java documentation we should give proper names to the methods and variables)

But we don't know how exactly the method will be implemented in it's (interface) implementation classes.

Following are the implementation classes for the interface Addicted.


The class AddictedToJava is providing implementation to the abstract method which returns the list of persons who were addicted to Java.



The class AddictedToAndroid is providing implementation to the abstract method which returns the list of persons who were addicted to Android.


Now , by looking at these implementations we came to know that the implementation is different in both of the implementation classes for the interface(Addicted).

Only after providing implementation, we came to know how exactly the method was implemented.

This is considered to be abstraction what i mentioned in the begining of the article.

Abstraction ------> Providing something.. but not everything  





51 comments:

  1. you cleared my doubts bro. I was asked the same question in an interview "what are we hiding when we are not implementing any logic in the abstract method".

    ReplyDelete
  2. Very informative buddy...

    ReplyDelete
  3. Nice tutorial...share your knowledge is caring.

    ReplyDelete
  4. great keep it up.but i need some extra information.

    ReplyDelete
  5. Abstraction in Java
    Real life Example of Abstraction

    Abstraction shows only important things to the user and hides the internal details. Real life example of abstraction is; we know how to ride the bike but we don't know internal function of bike.

    ReplyDelete
  6. Hello SP, thanks for ur support.
    I jus got a doubt, is abstraction to the developers or to the end users???

    It shud be for the end user only bcz however developers are aware of everything.

    If it is for the end user, then an end user even not aware of what is the implementation logic in concrete classes.

    So how can I say confidently to the end user that abstraction is achieved only by using abstract classes and interfaces??

    Thanks in advance:)

    ReplyDelete
    Replies
    1. Abstraction is for Implementer who uses your class.

      Delete
  7. Hello SP, thanks for ur support.
    I jus got a doubt, is abstraction to the developers or to the end users???

    It shud be for the end user only bcz however developers are aware of everything.

    If it is for the end user, then an end user even not aware of what is the implementation logic in concrete classes.

    So how can I say confidently to the end user that abstraction is achieved only by using abstract classes and interfaces??

    Thanks in advance:)

    ReplyDelete
  8. Hi Sajjawathussain,

    End user is the one who consumes the application and who doesn't bother about technical implementation.
    So for your question, not only abstraction, all the other OOP properties are for developers only.
    To write effective code and for effective utilisation we have all these properties..

    The main intention of using abstract classes /interfaces is to have a specific set of methods (which are abstract ) that needs to be implemented in their respective implementation classes in their own way..

    Hope you got it.. please feel free to get back in case of any confusion here.

    ReplyDelete
  9. i'm glad that this helped you pratikindia..

    ReplyDelete
  10. thans for reading this and liking it Rajeswari

    ReplyDelete
  11. Hi Pratap,

    In continuation to Sajjawathussain question and your reply.
    Why this concept is called abstraction when everything is known to the developer and end user doesn't bother about technical details.

    Thanks

    ReplyDelete
  12. thanks ur ans was helpful for me.

    ReplyDelete
  13. It's for developer only to reduce the complexity of the design. In abstract class some of the functionality you will implement and some of the functionality will be implemented by the concrete classor some of the method over-riden by concrete class. Ex. HttpServlet is a abstract class. We are extending HttpServlet class and we are overriding doGet() and doPost() method. How these methods are getting called?, How request and response is provided to doGet(req,res) and doPost(req,res) method? we are not aware about that this is abstraction.

    ReplyDelete
  14. what you hide in above example ??In interface you declared a method and provided it's implementation specific to a class implements it.Here what you hide??and how it is helpful?? you can use simply overriding mechanism why to declare it as interface and later to provide implementation??

    ReplyDelete
  15. Thanks It was helpful for me..!! keep sharing.

    ReplyDelete
  16. I have a doubt. in an interview I was asked->

    If abstraction is showing and encapsulation is hiding, whats the thin layer separating these two concepts?

    ReplyDelete
  17. Hi Pratap,
    I have a doubt actually. Suppose we want to use the method 'getAddictedListOfPersons()' defined in class 'AddictedToAndroid' , then I have to create the object of the class to use the method as below:
    Addicted adJava= new AddictedToAndroid();

    And while creating the object, the user will know the class which is implementing the method and will also know how the abstract method is being implemented. And thus there is no hiding from the user i.e. no abstration.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    rpa training in velachery| rpa training in tambaram |rpa training in sholinganallur | rpa training in annanagar| rpa training in kalyannagar

    ReplyDelete
  20. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

    ReplyDelete
  21. Impressive. Your story always bring hope and new energy. Keep up the good work.
    Python training in marathahalli | Python training institute in pune

    ReplyDelete
  22. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    java training in chennai | java training in electronic city

    java training in marathahalli | java training in btm layout

    ReplyDelete
  23. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    java training in chennai | java training in electronic city

    java training in marathahalli | java training in btm layout

    ReplyDelete
  24. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. R Programming institutes in Chennai | R Programming Training in Chennai

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us hadoop training in chennai velachery | hadoop training course fees in chennai | Hadoop Training in Chennai Omr

    ReplyDelete
  27. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Data science Related information please visit our website Data science courses in Pune page!

    ReplyDelete
  28. Hi,
    Best article, very useful and well explanation. Your post is extremely incredible.Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take Python Certification Course in Bangalore.


    ReplyDelete
  29. Thanks for your post.I gained some new important information.I am very happy reading your post.Learn Python,a powerful language used by sites like YouTube and Dropbox.
    python training in btm Layout

    ReplyDelete
  30. Your resume after a year of the job gains the place at the top position than the others. cursos de ti online

    ReplyDelete
  31. Want to do a No.1 Data Science Course in Chennai with a Certification Exam? Catch the best features of Data Science training courses with Infycle Technologies, the best Data Science Training & Placement institutes in and around Chennai. Infycle offers the best hands-on training to the students with the revised curriculum to enhance their knowledge. In addition to the Certification & Training, Infycle offers placement classes for personality tests, interview preparation, and mock interviews for clearing the interviews with the best records. To have all it in your hands, dial 7504633633 for a free demo from the experts.

    ReplyDelete
  32. Their say agreement operation some would. Market exist stock guess.entertainment

    ReplyDelete