0% found this document useful (0 votes)
13 views

L4 Abstraction

Abstraction in object-oriented programming (OOP) involves hiding unnecessary implementation details from users and only showing the essential attributes of an object. It reduces complexity and improves maintainability by showing only relevant details to users and hiding unrelated implementation details. Abstraction involves selecting important data from a larger pool of available data to display only what is relevant to users. This allows the same data to be used across different applications with minimal changes. Encapsulation is related to abstraction but involves binding together code and data into a single unit and controlling access to that data via public methods.

Uploaded by

singharushi948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

L4 Abstraction

Abstraction in object-oriented programming (OOP) involves hiding unnecessary implementation details from users and only showing the essential attributes of an object. It reduces complexity and improves maintainability by showing only relevant details to users and hiding unrelated implementation details. Abstraction involves selecting important data from a larger pool of available data to display only what is relevant to users. This allows the same data to be used across different applications with minimal changes. Encapsulation is related to abstraction but involves binding together code and data into a single unit and controlling access to that data via public methods.

Uploaded by

singharushi948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

L4 : What is Abstraction in OOP?

Abstraction is the concept of object-oriented


programming that “shows” only essential attributes and
“hides” unnecessary information.

The main purpose of abstraction is hiding the


unnecessary details from the users.

Abstraction is selecting data from a larger pool to show


only relevant details of the object to the user. It helps in
reducing programming complexity and efforts. It is one
of the most important concepts of OOPs.

Let’s Study Abstraction in OOPs with example:


Suppose you want to create a banking application and
you are asked to collect all the information about your
customer. There are chances that you will come up with
following information about the customer

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

Abstraction in Java
But, not all of the above information is required to create
a banking application.
So, you need to select only the useful information for your
banking application from that pool. Data like name,
address, tax information, etc. make sense for a banking
application which is an Abstraction example in OOPs

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

Since we have fetched/removed/selected the customer


information from a larger pool, the process is referred as
Abstraction in OOPs.
However, the same information once extracted can be
used for a wide range of applications. For instance, you
can use the same data for hospital application, job portal
application, a Government database, etc. with little or no
modification. Hence, it becomes your Master Data. This
is an advantage of Abstraction in OOPs.

Difference between Abstraction and


Encapsulation
Abstraction Encapsulation
Abstraction in Object Oriented
Encapsulation solves it
Programming solves the issues
implementation level.
at the design level.
Abstraction in Programming is
Encapsulation means
about hiding unwanted details
binding the code and data
while showing most essential
into a single unit.
information.

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

Encapsulation means
Data Abstraction in Java allows hiding the internal details
focussing on what the or mechanics of how an
information object must contain object does something for
security reasons.

Difference between Abstract Class and Interface


Abstract Class Interface
An abstract class can have both
The interface can have
abstract and non-abstract
only abstract methods.
methods.
It does not support multiple It supports multiple
inheritances. inheritances.
It can not provide the
It can provide the
implementation of the
implementation of the interface.
abstract class.
An abstract class can have An interface can have
protected and abstract public only have public abstract
methods. methods.
An abstract class can have final, The interface can only
static, or static final variable have a public static final
with any access specifier. variable.

What is Abstract Class?


Abstract Class is a type of class in OOPs, that declare
one or more abstract methods. These classes can have
abstract methods as well as concrete methods. A normal

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

class cannot have abstract methods. An abstract class is


a class that contains at least one abstract method.

What are Abstract Methods?


Abstract Method is a method that has just the method
definition but does not contain implementation. A method
without a body is known as an Abstract Method. It must
be declared in an abstract class. The abstract method will
never be final because the abstract class must implement
all the abstract methods.

Advantages of Abstraction
 The main benefit of using an Abstraction in
Programming is that it allows you to group several
related classes as siblings.
 Abstraction in Object Oriented Programming helps
to reduce the complexity of the design and
implementation process of software.

When to use Abstract Methods & Abstract Class?


Abstract methods are mostly declared where two or more
subclasses are also doing the same thing in different ways
through different implementations.
It also extends the same Abstract class and offers
different implementations of the abstract methods.
Abstract classes help to describe generic types of
behaviors and object-oriented programming class

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

hierarchy. It also describes subclasses to offer


implementation details of the abstract class.
Summary:
 Abstraction in Programming is the process of
selecting important data sets for an Object in your
software and leaving out the insignificant ones.
 Once you have modeled your object using Data
Abstraction in Java, the same set of data could be
used in different applications.

What is Encapsulation in Java?


Encapsulation in Java is a mechanism to wrap up
variables(data) and methods(code) together as a single
unit.
It is the process of hiding information details and
protecting data and behavior of the object.

It is one of the four important OOP concepts.

The encapsulate class is easy to test, so it is also better


for unit testing.

Learn Encapsulation with an Example


To understand what is encapsulation in detail consider
the following bank account class with deposit and show
balance methods
class Account {

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

private int account_number;


private int account_balance;

public void show Data() {


//code to show data
}

public void deposit(int a) {


if (a < 0) {
//show error
} else
account_balance = account_balance + a;
}
}
Suppose a hacker managed to gain access to the code
of your bank account. Now, he tries to deposit amount -
100 into your account by two ways. Let see his first
method or approach.
Approach 1: He tries to deposit an invalid amount (say
-100) into your bank account by manipulating the code.

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

Usually, a variable in a class are set as “private” as shown


below. It can only be accessed with the methods defined
in the class. No other class or object can access them.

If a data member is private, it means it can only be


accessed within the same class. No outside class can
access private data member or variable of other class.
So in our case hacker cannot deposit amount -100 to
your account.

Approach 2: Hacker’s first approach failed to deposit


the amount. Next, he tries to do deposit a amount -100
by using “deposit” method.

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

But method implementation has a check for negative


values. So the second approach also fails.

Thus, you never expose your data to an external party.


Which makes your application secure.

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

The entire code can be thought of a capsule, and you can


only communicate through the messages. Hence the
name encapsulation.

Data Hiding in Java


Data Hiding in Java is hiding the variables of a class
from other classes. It can only be accessed through the
method of their current class. It hides the implementation
details from the users. But more than data hiding, it is
meant for better management or grouping of related
data.

To achieve a lesser degree of encapsulation in Java, you


can use modifiers like “protected” or “public”. With
encapsulation, developers can change one part of the
code easily without affecting other.

Getter and Setter in Java

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

Getter and Setter in Java are two conventional


methods used to retrieve and update values of a variable.
They are mainly used to create, modify, delete and view
the variable values. The setter method is used for
updating values and the getter method is used for
reading or retrieving the values. They are also known as
an accessor and mutator.
The following code is an example of getter and setter
methods:
class Account {
private int account_number;
private int account_balance;
// getter method
public int getBalance()
{
return this.account_balance;
}
// setter method
public void setNumber(int num)
{
this.account_number = num;
}
}
In above example, getBalance() method is getter
method that reads value of variable account_balance
and setNumber() method is setter method that sets or
update value for variable account_number.

Abstraction vs. Encapsulation

Dr Bharti Sharma
L4 : What is Abstraction in OOP?

Often encapsulation is misunderstood with Abstraction.


Lets study-
 Encapsulation is more about “How” to achieve a
functionality
 Abstraction is more about “What” a class can do.
A simple example to understand this difference is a
mobile phone. Where the complex logic in the circuit
board is encapsulated in a touch screen, and the interface
is provided to abstract it out.

Advantages of Encapsulation in Java


 Encapsulation is binding the data with its related
functionalities. Here functionalities mean “methods”
and data means “variables”
 So we keep variable and methods in one place. That
place is “class.” Class is the base for encapsulation.
 With Java Encapsulation, you can hide (restrict
access) to critical data members in your code, which
improves security
 As we discussed earlier, if a data member is declared
“private”, then it can only be accessed within the
same class. No outside class can access data
member (variable) of other class.
 However, if you need to access these variables, you
have to use public “getter” and “setter” methods

Dr Bharti Sharma

You might also like