Abstraction - Java OOPs Concepts

Abstraction in OOPs

 is very easy to understand when you relate it to the real time example. For example, when you drive your car you do not have to be concerned with the exact internal working of your car. What you are concerned with is interacting with your car via its interfaces like steering wheel, brake pedal, accelerator pedal etc. Here the knowledge you have of your car is abstract.
In computer science, abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics) while hiding away the implementation details.
In more simple terms, abstraction is to hide information that is not relevant to context or rather show only relevant information and to simplify it by comparing it to something similar in the real world.
Typically abstraction can be seen in two ways:
  1. Data abstraction

    Data abstraction is the way to create complex data types from multiple smaller data types – which is more close to real life entity. e.g. An Employee class can be a complex object of having various small associations.
    public class Employee
    {
        private Department department;
        private Address address;
        private Education education;
        //So on...
    }
    So, if you want to fetch information of a employee, you ask it from Employee object – as you do in real life, ask the person itself.
  2. Control abstraction

    Control abstraction is achieved by hiding the sequence of actions for a complex task – inside a simple method call, so logic to perform the task can be hidden from the client and could be changed in future without impacting the client code.
    public class EmployeeManager
    {
        public Address getPrefferedAddress(Employee e)
        {
            //Get all addresses from database
            //Apply logic to determine which address is preferred
            //Return address
        }
    }
    In above example, tomorrow if you want to change the logic so that everytime domestic address is always the preferred address, you will change the logic inside getPrefferedAddress() method, and client will be unaffected.

Thank you for sharing Your Knowledge with others who need It.-Team LetML

Post a Comment

Copyright © LetML. Blogger Templates Designed by OddThemes