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  





55 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
    Replies
    1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

      Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

      Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

      The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  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. One of the best explanation.....

    ReplyDelete
  18. 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
  19. This comment has been removed by the author.

    ReplyDelete
  20. Best R Programming Training in Bangalore offered by myTectra. India's No.1 R Programming Training Institute. Classroom, Online and Corporate training in R Programming
    r programming training

    ReplyDelete
  21. 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
  22. 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
  23. Impressive. Your story always bring hope and new energy. Keep up the good work.
    Python training in marathahalli | Python training institute in pune

    ReplyDelete
  24. 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
  25. 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
  26. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  27. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

    best rpa training in chennai | rpa online training |
    rpa training in chennai |
    rpa training in bangalore
    rpa training in pune

    ReplyDelete
  28. 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
  29. This comment has been removed by the author.

    ReplyDelete
  30. 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
  31. 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.
    AWS Training in Electronic City

    ReplyDelete
  32. 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
  33. 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
  34. Your topic is very nice and helpful to us … Thank you for the information you wrote.

    Learn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
    Big Data Analytics Training in Bangalore
    Tableau Training in Bangalore
    Data Science Training in Bangalore
    Workday Training in Bangalore

    ReplyDelete
  35. 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
  36. Your resume after a year of the job gains the place at the top position than the others. cursos de ti online

    ReplyDelete