Data Abstraction, Control Abstraction In Java & OOPs

English meaning of abstraction

Something that exists only as an idea.

Technical translation

Abstraction is the concept of representing something at high level, without going into too much details.

Anyone involved into object oriented programming would be already aware of these definitions. However, it really helps to understand abstraction in depth to leverage its real benefits in your analysis and design.

Let us start by some real-life abstraction examples.

How abstraction makes life easy?

Consider that what you are planning for is – a week’s trip to visit few of your favorite destinations. And you got to plan for how you will be managing your accommodation, route, transportation, food and many more. You will want to have some travel agent to understand what you want and will take care about how to plan the trip. Here that Travel Agent is your abstraction to arrange your trip.

Another practical example of this can be seen in OSI network layers model. Don’t worry if you are not aware of it. Take any programming language e.g. Java. Java provides an abstraction layer for programmer so they don’t need to code in byte-codes of JVM or even machine-level language.

Here emphasis is on making client’s work easy by introducing one controlling abstraction. Additional layering hides complexity from current layer from the layers upwards in the stack.

Types of Abstraction

Most of the programming languages like C++, C#, Java supports two types of abstraction namely Control Abstraction and Data Abstraction.

Control Abstraction

Abstraction of behavior. Provides an easier, higher level API to hide client from unnecessary execution details. Note the emphasized words in the last sentence. Let’s look at both of them in detail.

Focus on “What”

As indicated by English meaning, abstraction indicates “only idea” – not associated with anything concrete. In other words, it focuses on what and factors out how something should be done.

Implementing abstraction in java

To implement abstraction in object oriented programming, you should be able to define just the behavior. With very limited or no implementation logic.

In java, this can be achieved by using abstract keyword at method and class level. By declaring accelerate() method as abstract in the Vehicle class below, you indicate that Vehicle is just an idea. Vehicle cannot be instantiated with out concrete implementation. And yes, every vehicle implementation must have some way to accelerate() or doBreak().

[codesnippet pb_margin_bottom=”yes” width=”1/1″ el_position=”first last”]public abstract class Vehicle{
public abstract void accelerate();
public abstract void doBreak();
public int getXPosition(){
// some implementation
}
}
[/codesnippet]

Taking abstraction at its extreme results into an interface. Interfaces only define the method signatures which must be implemented by any concrete implementation of that interface.

Abstract with perspective of the client

Any entity can be seen differently from different perspectives. And hence what is important from the perspective of client, may differ.

When an orthopedic doctor looks at patient,

abstraction-perspective1

Abstraction – Orthopedic Doctor

When dermatologist looks at the same patient

abstraction-perspective2

Abstraction – Dermatologist

So we see two different abstractions for the same entity(human).

Coming back to programming, consider a Car class as below.

[codesnippet pb_margin_bottom=”yes” width=”1/1″ el_position=”first last”]

public class Car {
private String color;
private String brand;
private String model;
private String speed;
private int xPosition;
private int yPosition;
// some accessors and behavior methods}

[/codesnippet]

Can you derive abstraction for this Car class? May sound like a weird question to some. Before you derive the abstraction, you must know what kind of system we are talking about. What client is really interested in the Car. Here are few examples of different abstractions from perspective of different systems.

Car Abstraction with perspective of Vehicle Tracking System

[codesnippet pb_margin_bottom=”yes” width=”1/1″ el_position=”first last”]

interface Trackable{

int getXCoordinate();
int getYCoordinate();

}

[/codesnippet]

Car Abstraction with perspective of Driver

[codesnippet pb_margin_bottom=”yes” width=”1/1″ el_position=”first last”]

interface Drivable{
void start();
void accelerate();
void reverse();
void doBreak();
String getSpeed();
void stop();
}

[/codesnippet]

Data Abstraction

Abstraction of data structures. Data abstraction refers to defining the behavior of the data structure. Data may be internally represented in different ways in concrete implementations. This data abstraction also includes various data types provided by programming languages, DBMS or language APIs.

In Programming Languages

Data types in programming languages is the best data abstraction example. boolean data type abstracts single bit data within it. It also adds behavior on this type to allow it to qualify as parameter in conditional operators. Similarly, int, float, double and other data types have specific behavior in language.

In DBMS

Database Management Systems have abstractions of Table and View. Every DBMS software may store underlying data differently. But to users, Table is always represented as  Rows and Columns. This allows performing high level operations like query on this data abstraction.

In Java Collections

Collection API’s Collection, List, Set and Map interfaces are example of data abstractions. These interfaces define some of the behavior characteristics to be implemented by concrete classes. For example, List defines characteristics and methods for random access from the underlying elements. Implementing class may choose to use array data structure for storing the data elements – ArrayList. Or it can be stored in any ad-hoc manner – LinkedList. Anything should be fine as long as the behavior defined by List interface is achieved.

2 Comments
Write a comment