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

6 Data Abstraction

This document discusses data abstraction in object-oriented programming and Java. It defines data abstraction as hiding irrelevant details and only exposing essential properties and behaviors. It provides examples like viewing a car as a car rather than its individual components. The document also discusses how abstraction is achieved in Java through interfaces and abstract classes, with interfaces providing 100% abstraction. It covers when to use abstract classes versus interfaces, with abstract classes allowing partial abstraction while interfaces require full abstraction.

Uploaded by

mafoxmentorship
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

6 Data Abstraction

This document discusses data abstraction in object-oriented programming and Java. It defines data abstraction as hiding irrelevant details and only exposing essential properties and behaviors. It provides examples like viewing a car as a car rather than its individual components. The document also discusses how abstraction is achieved in Java through interfaces and abstract classes, with interfaces providing 100% abstraction. It covers when to use abstract classes versus interfaces, with abstract classes allowing partial abstraction while interfaces require full abstraction.

Uploaded by

mafoxmentorship
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented

Programming 2(JAVA)
Data Abstraction
Introduction

 Data Abstraction is the property by virtue of which only the essential


details are displayed to the user.
 The trivial or the non-essentials units are not displayed to the user.
 Ex: A car is viewed as a car rather than its individual components.
 Data Abstraction may also be defined as the process of identifying
only the required characteristics of an object ignoring the irrelevant
details.
 The properties and behaviours of an object differentiate it from other
objects of similar type and also help in classifying/grouping the
objects.
Cont.:
 Consider a real-life example of a man driving a car. The man only
knows that pressing the accelerators will increase the speed of a car
or applying brakes will stop the car, but he does not know about how
on pressing the accelerator the speed is actually increasing, he does
not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car.

 This is what abstraction is.

 In java, abstraction is achieved by interfaces and abstract classes. We


can achieve 100% abstraction using interfaces.
Cont.:
 Another way, it shows only essential things to the user and hides

the internal details, for example, sending SMS where you type the

text and send the message. You don't know the internal processing

about the message delivery.

 Abstraction lets you focus on what the object does instead of how

it does it.
Abstract classes and Abstract methods :

 An abstract class is a class that is declared with an abstract keyword.


 An abstract method is a method that is declared without implementation.
 An abstract class may or may not have all abstract methods. Some of them can be
concrete methods
 A method defined abstract must always be redefined in the subclass, thus making
overriding compulsory OR either make the subclass itself abstract.
 Any class that contains one or more abstract methods must also be declared with an
abstract keyword.
 There can be no object of an abstract class. That is, an abstract class can not be directly
instantiated with the new operator.
 An abstract class can have parameterized constructors and the default constructor is
always present in an abstract class.
There are two ways to achieve abstraction in java

Abstract class (0 to 100%)


Interface (100%)
When to use abstract classes and
abstract methods
 There are situations in which we will want to define a superclass that
declares the structure of a given abstraction without providing a complete
implementation of every method.

 That is, sometimes we will want to create a superclass that only defines a
generalization form that will be shared by all of its subclasses, leaving it to
each subclass to fill in the details.
cont,.:
 Consider a classic “shape” example, perhaps used in a computer-
aided design system or game simulation.
 The base type is “shape” and each shape has a color, size, and so on.
From this, specific types of shapes are derived(inherited)-circle,
square, triangle, and so on — each of which may have additional
characteristics and behaviors.
 For example, certain shapes can be flipped. Some behaviors may be
different, such as when you want to calculate the area of a shape.
 The type hierarchy embodies both the similarities and differences
between the shapes.
Encapsulation vs Data Abstraction
 Encapsulation is data hiding(information hiding) while Abstraction is

detailed hiding(implementation hiding).

 While encapsulation groups together data and methods that act upon

the data, data abstraction deal with exposing the interface to the

user and hiding the details of implementation.


Advantages of Abstraction

 It reduces the complexity of viewing the things.

 Avoids code duplication and increases reusability.

 Helps to increase the security of an application or

program as only important details are provided to

the user.
Difference
No. Class Interface
1. In class, you can instantiate In an interface, you can’t
variables and create an instantiate variables and
object. create an object.

2. Class can contain The interface cannot contain


concrete(with concrete(with
implementation) methods implementation) methods

3. The access specifiers used In Interface only one specifier


with classes are private, is used- Public.
protected, and public.
En d ! ! !
!! !

You might also like