BIG
DATA

JAVA

ABSTRACTION IN OOP WITH EXAMPLES

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

Information Hiding

In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. The protection involves providing a stable interface which protects the remainder of the program from the implementation (the details that are most likely to change).

Information hiding is the ability to prevent certain aspects of a class or software component from being accessible to its clients, using programming language features. Information hiding is a powerful programming technique because it reduces complexity.

Information hiding is one of the most important principles of OOP. By Information Hiding we mean "Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world."

Real World Examples of Information Hiding

Lets see an simple example of Information Hiding:
Your name and other personal information is stored in your brain we can't access this information directly. For getting this information we need to ask you about it and it will be up to you how much details you would like to share with us.

In object oriented programming approach we have objects with their attributes and behaviors that are hidden from other classes, so we can say that object oriented programming follows the principle of information hiding.

In the perspective of Object Oriented Programming Information Hiding is,
"Hiding the object details (state and behavior) from the users"
Here by users we mean "an object" of another class.

Information Hiding is achieved in Object Oriented Programming using the following principles,

  • All information related to an object is stored within the object
  • It is hidden from the outside world
  • It can only be manipulated by the object itself

We can achieve information hiding using Encapsulation and Abstraction, so lets see these two concepts in detail.



Abstraction

Abstraction is the process of hiding out the working style of an object and showing only the required information of the object in understandable manner.

A radio has a tuner, an antenna, a volume control, and an on/off switch. To use it, you don't need to know that the antenna captures radio frequency signals, converts them to electrical signals, and then boosts their strength abstraction via a high-frequency amplification circuit. Nor do you need to know how the resulting current is filtered, boosted, and finally converted into sound. You merely turn on the radio, tune in the desired station, and listen. The intrinsic details are invisible. This feature is great because now everyone can use a radio, not just people with technical know-how. Hiring a consultant to come to your home every time you wanted to listen to the radio would become awfully expensive. In other words, you can say that the radio is an object that was designed to hide its complexity.

Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking, Abstraction is hiding the complexities of your class or struct or in a generic term Type from outer world. This is achieved by means of access specifiers.

To me abstraction is the way we see things for example in my car I never know that how many devices are used in my engine, how petrol burns, how gear system changes the speed of my car, I only knows that when I press accelator my car moves fast and when I press breaks my car slows down and I can adjust speed of my car using gears.

Similarly in object oriented world, we always try to hide the complexity of a system from its end user e.g. I have a chat system which interacts with another chat client. Then i will try to create a abstraction layer which will hide all the complexity of TCP/IP protocols and give user simple functions like SendMessage or RecieveMessage.



Why Abstraction?

One of the fundamental reasons for engaging in the task of abstraction in software analysis, design and development is to reduce the complexity to a certain level so that the relevant aspects of the requirements, design and development may be easily articulated and understood.

Definitions:

Following are the different definations of abstraction you could use if asked for in your interview:

  • Hiding the complexities of your type from outside world.
  • Abstraction is the process of hiding out the working style of an object and showing only the required information of the object in understandable manner.
  • Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.
  • Abstraction is "To represent the essential feature without representing the back ground details."
  • Abstraction lets you focus on what the object does instead of how it does it.
  • Abstraction provides you a generalized view of your classes or object by providing relevant information.
  • Abstraction is process of hiding the implementation details and showing only the functionality.

My favorite one is : Abstraction lets you focus on what the object does instead of how it does it.



Example

[1] You know that you need to put on the keys, change the gear and accelerate in order to drive a car but do you actually know what happens when you change the gear and accelerate, or say how the car engine works? You don't, and that's simple because you don't need to know all that complexity. Your Job is to just drive the car. That's what we call Abstraction.

[2] Lets take a person as example and see how that person is abstracted in various situations

  • A doctor sees (abstracts) the person as patient. The doctor is interested in name, height, weight, age, blood group, previous or existing diseases etc of a person
  • An employer sees (abstracts) a person as Employee. The employer is interested in name, age, health, degree of study, work experience etc of a person.

[3] Monitor ON/OFF
The user doesn't need to know much about all the chips functioning that happens when Monitor is switched ON or OFF..All he needs to know is On Function ON-Monitor is On and on function OFF-Monitor is off.



Abstraction Layers/Levels

An abstraction layer is a mechanism that separates two complex systems. In computing, an abstraction layer or abstraction level is a way of hiding the implementation details of a particular set of functionality, allowing the abstraction-layerseparation of concerns to facilitate interoperability and platform independence or to modularize a software system for later expansion.

A typical vision of a computer architecture as a series of abstraction layers: hardware,firmware, assembler, kernel,operating system and applications.

Example

When we see a nice car on the road as a casual onlooker, we get to see the whole picture. The car as a one single unit, a vehicle. We do not see the underlying complex mechanical engineering.

Now consider we are going to a showroom to buy a car. What do we see now? We see four wheels, powerful engine, power steering etc. We see the car at high level components. But, there is so much inside it which gives the completeness to the car.

Now consider a mechanic, who is going to service the car. He will see one more level deeper with more level of information.

When we design software, we take the context. In the above example, we ask the question whether we are designing the software for a causal on looker or a buyer or a mechanic? Levels of abstraction is applied on the design accordingly.



Abstraction in OOP

In general computer software, when we talk about abstraction the software language itself is an example for the concept of abstraction. When we write a statement as,

a = b + c;

we are adding two values stored in two locations and then storing the result in a new location. We just describe it in an easily human understandable form. What happens beneath? There are registers, instruction sets, program counters, storage units, etc involved. There is PUSH, POP happening. High level language we use abstracts those complex details.

When we say abstraction in Java, we are talking about abstraction in object oriented programming (OOP) and how it is done in Java. Concept of abstraction in OOP, starts right at the moment when a class is getting conceived. I will not say, using Java access modifiers to restrict the properties of an object alone is abstraction. There is lot more to it. Abstraction is applied everywhere in software and OOP.

Abstraction in Java

Having read the above section, you might have now come to an idea of how abstraction is done in Java

  • When we conceptualize a class
  • When we write an interface
  • When we write an abstract class, method
  • When we write extends
  • When we apply modifiers like private


So when do you use abstraction?

when You know something needs to be there but not sure how exactly it should look like.
e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons, the same concept apply to interface in Java also.
so implementation of those start() and stop() methods should be left to their concrete implementation e.g. Scooter , MotorBike , Car etc.
Java Interface is an another way of providing abstraction, Interfaces are by default abstract

Where should we use abstract class and where should we use Java Interfaces?

you can go for java interface if you only know the name of methods your class should have e.g. for Server it should have start() and stop()method but we don't know how exactly these start and stop method will work. if you know some of the behavior while designing class and that would remain common across all subclasses add that into abstract class. Interface like Runnable are good example of abstraction in Java which is used to abstract task executed by multiple thread.