0% found this document useful (0 votes)
100 views14 pages

Abstraction Data Hiding Encapsulation in OOP

Data abstraction, data hiding, and encapsulation are key principles of object-oriented programming. Data abstraction involves focusing only on essential characteristics of an object to reduce complexity. Data hiding is achieved by declaring class variables as private and providing public getter and setter methods. Encapsulation binds together data and methods that operate on the data within a single unit (class). It involves data hiding and hides implementation details by making variables private and accessing them only via public methods.
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)
100 views14 pages

Abstraction Data Hiding Encapsulation in OOP

Data abstraction, data hiding, and encapsulation are key principles of object-oriented programming. Data abstraction involves focusing only on essential characteristics of an object to reduce complexity. Data hiding is achieved by declaring class variables as private and providing public getter and setter methods. Encapsulation binds together data and methods that operate on the data within a single unit (class). It involves data hiding and hides implementation details by making variables private and accessing them only via public methods.
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/ 14

Data Abstraction, Data Hiding

and
Encapsulation in OOP

Dr. Kuppusamy .P
Associate Professor / SCOPE
Data Abstraction
• Abstraction is the process of considering essential characteristics of an object that
distinguish it from all other objects.
Need of Abstraction
• Abstraction focuses only on most relevant data of an object to the specific process.
• It hides the background details to reduce the complexity and increase efficiency.
• Offers the greatest flexibility when using ADT(Abstract Data Type) objects in
different situations.
✓ ADT is a type (or class) for objects whose behavior is defined by a set of values and a
set of operations.
✓ ADT specifies only what operations are to be performed but not how these operations
will be implemented.
✓ Also, not specifies how data will be organized in memory and what algorithms will be
used for implementing the operations. It is called “abstract” because it gives an
implementation-independent view.
✓ The process of providing only the essentials and hiding the details is known as
abstraction.
• In java, abstraction is achieved by interfaces and abstract classes. Users can
achieve 100% abstraction using interfaces.
Dr. Kuppusamy P
Abstract classes
➢ Abstract is a non-access modifier in java and applicable for classes, methods but
not variables.
➢ An abstract class is a class that is declared with abstract keyword.
abstract class class-name{
//body of class
}
➢ Abstract class is a restricted class that cannot be used to create objects (to access
abstract class, it must be inherited from another class)
➢ An abstract class may or may not have all abstract methods. Some of them can be
concrete methods.
➢ An abstract class can be instantiated either by a concrete subclass or by defining
all abstract methods along with the new keyword.

Dr. Kuppusamy P
Abstract methods
➢ An abstract method is a method that is declared without implementation.
abstract type method-name(parameter-list);
➢ Abstract method can only be used in an abstract class, and it does not have a body. The
body is provided by the subclass (inherited from).
➢ Thus, making overriding compulsory.
➢ Any class that contains one or more abstract methods must also be declared with abstract
keyword.
➢ An abstract class can have parameterized constructors, and default constructor is always
present in an abstract class.
➢ Concrete Class: A concrete class is a subclass which implements all the abstract method
of its super abstract class. It also has implementations of all methods of interfaces it
implements.
➢ Concrete Method: The methods which are not abstract methods are concrete methods. To
execute concrete methods, create an instance (object) of the class and call to that specific
method.
Dr. Kuppusamy P
Abstract class
• abstract class cannot be instantiated directly
• E.g.,
abstract class Demo {
abstract void display();
}

public class App {


public static void main(String[] args)
{
Demo AC = new Demo();
System.out.println("Hello");
}}

Then, How to instantiate?


Dr. Kuppusamy P
Abstract methods
• An abstract class can be instantiated either by a concrete subclass or by defining
all abstract methods along with the new keyword.

abstract class Demo {


abstract void display();
}
class A extends Demo
{
void display()
{
System.out.println("Hello");
}
}
public class App {
public static void main(String[] args)
{
A obj = new A();
obj.display();
System.out.println(“Welcome to VIT AP");
}
} Dr. Kuppusamy P
Abstract methods
• An abstract class can be instantiated either by a concrete subclass or by defining
all abstract methods along with the new keyword.

abstract class Demo {


abstract void display();
}

public class App {


public static void main(String[] args)
{
Demo AC = new Demo() {
void display()
{
System.out.println("Hello!!!");
}
};
AC.display();
System.out.println(“Welcome to VIT AP");
}
Dr. Kuppusamy P
}
Abstract classes
• An abstract class can be instantiated either by a concrete subclass or by defining all the
abstract method along with the new statement.

abstract class Addition


{ void add() {
// abstract with method and it has no body System.out.println(“implementation of add().");
abstract void add(); }
void method() }
{ // Driver class due to have main()
System.out.println(“Its concrete method."); public class AbstractEx
} {
} public static void main(String args[])
// concrete class sum {
class Sum extends Addition Sum b = new Sum();
{ b.add();
// class sum must override add() method b.method();
// otherwise, compile-time exception occurs }
}
Dr. Kuppusamy P
Data Hiding
• Hiding an internal object details i.e., data members within the class to prevent its direct
access from outside the class.
• Data hiding ensures exclusive data access to class members.
• It protects object integrity by preventing unintended or intended changes.
• Data hiding also reduces system complexity by limiting interdependencies between
software components.
• It increases the security.
• Data hiding can be achieved by
• Declare the variables of a class as private.
class Bank { // Setter method
// Private data (data hiding) void set_balance(long amt, long no)
private long bal = 0; {
//getter method //validate the customer
public long get_balance(long no) }
{ }
//validate the customer
}
Dr. Kuppusamy P
Data Hiding
class Bank { if (this.Accno == no) {
// Private data (data hiding) bal = bal + amt;
private long bal = 0; }
long Accno=456; }
//getter method }
public long get_balance(long no) public class Customer {
{ public static void main(String[] args)
//validate the customer {
if (this.Accno == no) { Bank cust = new Bank();
return bal; int cno = 456;
} cust.set_balance(1000, cno);
// Unauthorised customer long balance = cust.get_balance(cno);
return -1; System.out.println("Account" + " " + cno);
} System.out.println("Current Balance"+ " " + balance);
// Setter method }
public void set_balance(long amt, long no) }
{ //validate the customer
Dr. Kuppusamy P
Encapsulation
• Encapsulation is binding data (member variables) and operations (methods) that
work on data together into a single unit (class).
• In encapsulation, the variables of a class will be hidden from other classes. So,
users cannot access state values of all variables of a particular object.
• These variables can be accessed only through the methods of their current class
called data hiding.
• Encapsulation involves Data hiding and Implementation Hiding (Abstraction).
• Encapsulation hides the implementation level details i.e., hide both data members
and functions or methods associated with an instantiated class or object.
• To achieve encapsulation in Java
• Declare the variables of a class as private.
• Use public access specifier for methods to modify and view the variables
values. Dr. Kuppusamy P
Encapsulation
class Encap { ssn = newValue;
private int ssn; }
private String empName; }
private int empAge;
public class EncapTest{
//Getter and Setter methods public static void main(String args[]){
public int getEmpSSN(){ Encap obj = new Encap();
return ssn; obj.setEmpName("Mario");
} obj.setEmpSSN(112233);
public String getEmpName(){
return empName; System.out.println("Employee Name: " +
} obj.getEmpName());
System.out.println("Employee SSN: " +
public void setEmpName(String newValue){ obj.getEmpSSN());
empName = newValue; }
} }

public void setEmpSSN(int newValue){

Dr. Kuppusamy P
Encapsulation vs Data Abstraction

1. Encapsulation is data hiding(information hiding) while Abstraction is detail


hiding(implementation hiding).
2. Data hiding only hides class data components, whereas data encapsulation hides
class data parts and methods.
3. While encapsulation groups together data and methods that act upon the data, data
abstraction deals with exposing the interface to the user and hiding the details of
implementation.
Advantages
1. It reduces the complexity of viewing the things.
2. Avoids code duplication and increases reusability.
3. Helps to increase security of an application or program as only important details
are provided to the user.

Dr. Kuppusamy P
References

• Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth


edition, 2017.
• Joyce Farrell, “Java Programming”, Cengage Learning, Eighth Edition, 2016.
• Mark Lassoff, “Java Programming for Beginners”, Pack Publishing, 2017

Dr. Kuppusamy P

You might also like