You are on page 1of 2

Abstraction

Abstraction is another good feature of OOPS. Abstraction means to show only the necessary details to the client of the object.

Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what
is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT. When you change the gear of
your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get
changed that’s it!! This is abstraction; show only the details which matter to the user.

Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary
of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about
how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and
format of resulting member, Right?

So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your
class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This certainly
helps in reusability of the code.
As I have generally seen developers are not very much comfortable with the database programming. Let’s say you are designing a
class that is used to interact with the database and to perform some of database operations. Now client of your class need not to be
aware of database programming, he just need to be aware of some of the details of your class and easily can perform the database
operations exposed by your class without deep knowledge of database programming.
The best thing of abstract is that this decouples the user of the object and its implementation. So now object is easy to understand and
maintain also. As if there is any change in the process of some operation. You just need to change the inner details of a method,
which have no impact on the client of class.

Or

Abstraction is a way to remove the association of the behavior of an object with the actual details behind the scenes which implement
that object's behavior.

This 'abstraction' is usually accomplished through the use of base classes with virtual functions; each derived function provides the
details that implement the behavior behind that abstraction.

A simple example is using a base class "Animal", with a virtual function "Walk". In the case two-legged versus four-legged animals,
both of them walk, but the actual mechanics are different. The "Walk" method abstracts the actual mechanics behind the walking that
each "Animal" does.

Difference between Encapsulation & Abstraction OOPS


Abstraction is virtual class design.
Before actually defining class, developer will think about what all properties, methods and event will be there in my class.
Whereas, encapsulation is data hiding.
At the time of class defenation, developer will think about which should display to end user and which should not.
In Short Abstraction is "Collection of data" and Encapsulation is "Exposure (or grouping) of data in appropriate access specifier".

Brief Introduction to OOP


Object Oriented Programming or OOP is the technique to create programs based on the real world. Unlike procedural programming,
here in the OOP programming model programs are organized around objects and data rather than actions and logic. Objects represent
some concepts or things and like any other objects in the real Objects in programming language have certain behavior, properties,
type, and identity. In OOP based language the principal aim is to find out the objects to manipulate and their relation between each
other. OOP offers greater flexibility and compatibility and is popular in developing larger application. Another important work in
OOP is to classify objects into different types according to their properties and behavior. So OOP based software application
development includes the analysis of the problem, preparing a solution, coding and finally its maintenance.
Java is an object oriented programming and to understand the functionality of OOP in Java, we first need to understand several
fundamentals related to objects. These include class, method, inheritance, encapsulation, abstraction, polymorphism etc.

Class - It is the central point of OOP and that contains data and codes with behavior. In Java everything happens within class and it
describes a set of objects with common behavior. The class definition describes all the properties, behavior, and identity of objects
present within that class. As far as types of classes are concerned, there are predefined classes in languages like C++ and Pascal. But
in Java one can define his/her own types with data and code.

Object - Objects are the basic unit of object orientation with behavior, identity. As we mentioned above, these are part of a class but
are not the same. An object is expressed by the variable and methods within the objects. Again these variables and methods are
distinguished from each other as instant variables, instant methods and class variable and class methods.

Methods - We know that a class can define both attributes and behaviors. Again attributes are defined by variables and behaviors are
represented by methods. In other words, methods define the abilities of an object.

Inheritance - This is the mechanism of organizing and structuring software program. Though objects are distinguished from each
other by some additional features but there are objects that share certain things common. In object oriented programming classes can
inherit some common behavior and state from others. Inheritance in OOP allows to define a general class and later to organize some
other classes simply adding some details with the old class definition. This saves work as the special class inherits all the properties
of the old general class and as a programmer you only require the new features. This helps in a better data analysis, accurate coding
and reduces development time.

Abstraction - The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In
other words, it deals with the outside view of an object (interface).

Encapsulation - This is an important programming concept that assists in separating an object's state from its behavior. This helps in
hiding an object's data describing its state from any further modification by external component. In Java there are four different terms
used for hiding data constructs and these are public, private, protected and package. As we know an object can associated with data
with predefined classes and in any application an object can know about the data it needs to know about. So any unnecessary data are
not required by an object can be hidden by this process. It can also be termed as information hiding that prohibits outsiders in seeing
the inside of an object in which abstraction is implemented.

Polymorphism - It describes the ability of the object in belonging to different types with specific behavior of each type. So by using
this, one object can be treated like another and in this way it can create and define multiple level of interface. Here the programmers
need not have to know the exact type of object in advance and this is being implemented at runtime.

You might also like